Solved

C# Forms Collection in Class

Posted on 2016-08-12
14
66 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 69

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
 
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 69

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 69

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 69

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 69

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

896 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now