Search code examples
c#winformsoopmdichildmdiparent

How to use ChildForm data and component from the parentChild.? Window Form MDI


I have a project with a form MDIContainer (form1) and this form1 has 4 child forms ( form2, form3, form4, form5).

All the data need to be connected, I mean, I need create one object of each form and don't lose the data of this object

In the form1, I declare all the forms like: form2 frm_2 = new form2 ...

form2,form3,form4,form5 has DataGridViews, bindingsources, etc...

Some times I need manipulate data between forms.

ex:How Can I pass data through the childforms ? But this data need to be always there

Ex:. When on the form2 the event DataGridView2_SelectionChanged I need get some data from the database and put in form3 DataGridView3.But on the form3 I have a method and bindingsource and dataset binding on the DataGridView3. So I need access this method.

I need a example how to do that, many child forms and How to communicate between all of them


Solution

  • This has been asked many many times and received enough answers to help you out.

    (Which is why you got a downvote from somebody. You are expected to do a proper search before you ask anything!)

    With the setup you described you can go this way: Hand a reference to the main form to each child form in the constructor. Keep this reference and use it to access both the main form and via its many form handles all the other child forms.

    Form23 form23 = new Form23(this);
    form23.Show();
    

    And in Form23..:

    // a class variable:
    public Form1 form1;
    
    public Form23(Form1 form1_);
    {
        InitializeComponent();
        form1 = form1_;
    }
    

    Now you can use form1 as a hub and reference each sibling like this:

    form1.form22.somePublicDataFieldOrWhatever...