Solved

C# Forms Collection in Class

Posted on 2016-08-12
14
57 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 74

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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:…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

20 Experts available now in Live!

Get 1:1 Help Now