Solved

C# Forms Collection in Class

Posted on 2016-08-12
14
72 Views
Last Modified: 2016-08-14
I need to pass collections to a form from a class.  I created a public method in the form.  I was going to write the following code
FormsCollection fc = Application.OpenForms;

foreach (var f in fc)
{ 
  if (f.GetType() == typeof(MyForm)f;
      form.CallMyMethod;
  }
}

Open in new window

I ether need to do that or I need to open a form from a class.

But it seems like I can't do that in a class.  Any other ideas?
0
Comment
Question by:CipherIS
14 Comments
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 83 total points
ID: 41753610
I think you only are missing 1 small detail:

FormsCollection fc = Application.OpenForms;

foreach (var f in fc)
{ 
  if (f.GetType() == typeof(MyForm)f;
     MyForm mf = (MyForm)f;
     mf.CallMyMethod;
  }
}

Open in new window

0
 
LVL 1

Author Comment

by:CipherIS
ID: 41753757
When I type FormsCollection it doesn't like that in the class
0
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 251 total points
ID: 41753767
you need to add a using clause for System.Windows.Forms (and probably a reference to it as well)
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 83 total points
ID: 41753808
You will need a using statement for System.Windows.Forms and add reference to System.Windows.Forms.dll if it is in a different project that already does not have that reference
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 83 total points
ID: 41753892
Why not just?

Application.OpenForms
           .OfType<MyForm>()
           .ToList()
           .ForEach(form => form.CallMyMethod());

Open in new window

0
 
LVL 1

Author Comment

by:CipherIS
ID: 41753928
Ok, adding the reference to Windows.Forms helped but now when I add the name of my form it won't find it as I need to add a reference to it.  My class is in another project and I can't add the project that my form is in because it would create a circular reference.

Any ideas?
0
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 251 total points
ID: 41753958
can you create an interface? or should you use Reflection (but then you loose all intellisense and compilation errors)?
0
 
LVL 1

Author Comment

by:CipherIS
ID: 41753977
Unsure how to create an interface to get it to work now.  What I did was call my method and pass my objects with reference.

Excuse the rude code.
MyObject o = new MyObject();

o.MyMethod(ref ClassObject);

MyObject.MyMethod(ref ClassObject classObject)

Open in new window

The problem that I'm having is that the changes in my method are not pushing back up.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41753990
>>The problem that I'm having is that the changes in my method are not pushing back up.

What? Have you done a full rebuild? Are your 2 projects in the same solution?

For the interfaces, check https://msdn.microsoft.com/en-us/library/ms173156.aspx
0
 
LVL 1

Author Comment

by:CipherIS
ID: 41753993
They are in the same solution
0
 
LVL 1

Author Comment

by:CipherIS
ID: 41753999
I don't understand how the interface would help me.  Why isn't the ref passing the values back?
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41754031
an interface would avoid circular reference yet letting use early binding.

but before going further, are you sure your architecture is good? Why a class which in theory knows nothing about the forms should call a method in it?
0
 
LVL 1

Author Comment

by:CipherIS
ID: 41754078
I need to get better with interfaces.  Unfortunately, I don't have time to rewrite it.  I was able to get the ref to work.  

I'm curious how to implement the interface to get this to work as you are thinking.
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 251 total points
ID: 41755513
I just found some time to provide a sample using an interface.

First create an interface:
namespace WindowsFormsApplication1
{
    interface IMyInterface1
    {
        void DoSomething();
    }
}

Open in new window


Then create your class:
using System.Linq;

namespace WindowsFormsApplication1
{
    public static class Class1
    {
        public static void DoSomethingGeneric()
        {
            System.Windows.Forms.Application.OpenForms
           .OfType<IMyInterface1>()
           .ToList()
           .ForEach(form => form.DoSomething());
        }
    }
}

Open in new window


And finally your form:
using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form, IMyInterface1
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void DoSomething()
        {
            MessageBox.Show("Interface implementation!");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Class1.DoSomethingGeneric();
        }
    }
}

Open in new window

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Adding  DYMO Labelprinter to c# client application 4 29
in Dot net,lastest version of MVC 3 23
Batch file or script with password 22 39
location of a form 2 10
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question