Question

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

Was it helpful?

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...

OTHER TIPS

Thank you for your answer.
This is how I'm doing now:
In the Main Form:

public partial class Form1 : Form
{
 public Form2 form2;
    public Form3 form3;
    public Form4 form4;
    public Form5 form5;

    public Form1()
    {
        InitializeComponent();

         form2 = new Form2(this);

         form3 = new Form3(this);

         form4 = new Form4(this);

         form5 = new Form5(this);
    }
   private void Form1_Load(object sender, EventArgs e)
    {


        form2.MdiParent = this;
        form3.MdiParent = this;
        form4.MdiParent = this;
        form5.MdiParent = this;
        form2.Show();
        form3.Show();
        form4.Show();
        form5.Show();
}
}

Then Each child form have a reference to the main, like the @Taw said:

public partial class Form2 : Form
{
    public Form1 form_1;

    public Form2(Form1 form1)
    {
        InitializeComponent();
        form_1 = form1;
    }
}

And now I can do something like that:

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
    {
       if(dataGridView1.Rows.Count> 1)
        form_1.form2.UpdateDataGrid(Convert.ToInt16(dataGridView1.SelectedRows[0].Cells[0].Value));
    }

Maybe it can help someone... if anyone knows a better way, please share.

Sorry if this it was duplicate question, but in my research I don't seen something like that. Maybe I have not researched enough. =]

Thank you @TaW for your consideration

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top