jerbotron
jerbotron

Reputation: 307

Communicating between multiple windows forms in c#

I'm working on a program that involves multiple forms triggered by buttons, events etc. And I'm having trouble passing and changing objects through multiple forms.

For example, say I have form 1, form 2, and form 3.

I have a list defined in form 1 but all 3 forms need to have the ability to change the values in that list?

I'm guessing it's not as simple as just writing:

Form1 frm1 = new Form1(); 

Form2 frm2 = new Form2();

etc...

What's a better way to do so?

Upvotes: 1

Views: 1938

Answers (7)

Hamza_L
Hamza_L

Reputation: 654

I think there are two ways to proceed, and it depends on the list :

  • the list is a global variable, in this case, I think that you have de build a static class and put your list in it to be accessible for all.
  • the list is not global, here you have to override the constructors of your forms et pass the list in every constructor.

Upvotes: 2

Jaska
Jaska

Reputation: 1430

Just make the list public if you don't want static list, then check from Application.OpenForms for the open form

if (Application.OpenForms["Form1"] != null)
{
    ((Form1)Application.OpenForms["Form1"]).list[0] = 5;
}

Upvotes: 1

Jean-Christophe Fortin
Jean-Christophe Fortin

Reputation: 748

You are actually doing what MVC pattern try to solve. You'll need Models (your list or else), Views (your forms) and Controller. That way every views will be able to share/access data by using the controller

Upvotes: 1

Steve
Steve

Reputation: 216353

You could build a static global class that keeps all of your global accessible data. Or you can pass the List created in first form to each subform via their constructors

For example: (in form1)

Form2 frm = new Form2(myList);

in form2

public class Form2:Form
{
    List<MyObjects> _globalList = null;

    public Form2(List<MyObjects> listData)
    {
        InitializeComponent();
        _globalList = listData;
    }

}

then inside Form2 you can use the list passed and change/add/delete its elements. These updates will be visible also in Form1. Of course the same method could be applied when opening Form3.

The other solution requires the construction of a global static class dedicated to contains global values used everywhere in you application. If used with caution could be very useful.

In a GlobalData.cs file

public static class MyAppData
{
     public static List<MyObjects> GlobalData { get; set; }
}

in Form1

MyAppData.GlobalData = new List<MyObjects>();
MyAppData.GlobalData.Add(new MyObjects(......));

and in Form2

if(MyAppData.GlobalData.Contains(.......))

Upvotes: 1

You don't need any static members to get this done. I would suggest something lile the following:

  1. Instantiate the list that will be accessible to all your forms. If you will data-bind some controls (listboxes, comboboxes, ...) to it, look into the BindingList type.

  2. Have the forms' constructors accept a list which they will use e.g. as a data source to bind against.

    var list = new BindingList(...);
    var form1 = new Form1(list);
    var form2 = new Form2(list);
    
  3. If your forms need to be opened all simultaneously, show them modelessly using the Show method instead of ShowDialog.

Upvotes: 2

SamFisher83
SamFisher83

Reputation: 4015

Use a Singleton pattern?

http://en.wikipedia.org/wiki/Singleton_pattern

Upvotes: 1

nxu
nxu

Reputation: 2272

I assume you're creating the Form2 and Form3 from the Form1. You can simply make a property in these forms and pass the list to it (maybe with a constructor). Since lists are reference types, modifying it in Form2 would modify it in Form 1 as well.

Upvotes: 1

Related Questions