Solved

C# Forms Collection in Class

Posted on 2016-08-12
14
88 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 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 63

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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:…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

733 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