?
Solved

C# Forms Collection in Class

Posted on 2016-08-12
14
Medium Priority
?
104 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
14 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 332 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 1004 total points
ID: 41753767
you need to add a using clause for System.Windows.Forms (and probably a reference to it as well)
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 332 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 332 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 1004 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 1004 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

752 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