Andrew Day
Andrew Day

Reputation: 125

C# Updating Form1 when closing Form2

I have a situation where a user clicks a button on Form1 to open Form2. Form1's load event populates a treeview based on data returned from the database. When they input data into Form2, the database updates, but the treeview does not. If I close and reopen the winform, then it pulls in the new data.

I have been digging for a few hours now, and I found out how to call the getdata method in Form1 from Form2, but it doesn't work. I can trace it in debug mode, and verified my dataReader contains the new value, but the treeview does not update. I did it like this.

//In form 2
Form1 frm = new Form1();
frm.getData();

Nothing happens. If I say frm.Show() then it opens a new Form1 window with the updated data, but I want the treeview to update in the already opened Form1 window.

I believe this is the root of the problem. Any call I make to frm happens in a new window. How do I access the existing Form1 window's getData() method from Form2?

Upvotes: 1

Views: 4753

Answers (5)

Heidi
Heidi

Reputation: 11

The best way I found to complete this task is to have a method on form1 that is called after the form2 modal is closed.

Form1

private void addButton_Click(object sender, EventArgs e)
{
    // Create an instance of form 2
    Form2 secondForm = new Form2();

    // Display form 2
    secondForm.ShowDialog();

    // Call a method on form1 to update information on form 1. Any code listed after 
    // the show dialog call will process after Form2 is closed.
    UpdateForm1();
}

private void UpdateForm1()
{
    // Code your updates to form1 here
}

Upvotes: 1

MGE
MGE

Reputation: 912

Have a look:

//In Form1 opening Form2
Form2 frm = new Form2();
frm.Owner = this;
frm.Show();

//Exemple to call functions to FORM1 from FORM2
private void button1_Click(object sender, EventArgs e)
{
    Form1 frmParent = (Form1)this.Owner;
    frmParent.testeFunction();
    frmParent.InsertInGrid(textBox1.Text);
}

So, basically you need to create one function in Form1 to call from Form2 (passing Parameters). I hope this help

Upvotes: 0

saeed
saeed

Reputation: 2497

All the thing I understand is that you want to have a reference to the form1 that already exists. you might use Application to have any access to the forms.

like this code:

 private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            Form1 myfrm =Application.OpenForms["Form1"] as Form1;
            myfrm.GetData();
        }

Upvotes: 1

Mark Hall
Mark Hall

Reputation: 54532

Without knowing how you are creating Form2 and showing it in Form1, i.e. are you using .Show or .ShowDialog if it is the later all you have to do is use the DialogResult to determine whether or not to call your getDatamethod.

Form2 frm2 = new Form2();
if (frm2.ShowDialog() == DialogResult.OK)
{
    getData();
}

If you are using the Show Method you can listen for Form2's FormClosing or FormClosed event and respond to that in your Form1. Like this.

private void button1_Click(object sender, EventArgs e)
{
    Form2 frm2 = new Form2();
    frm2.FormClosed += frm2_FormClosed;
    frm2.Show();
}

void frm2_FormClosed(object sender, FormClosedEventArgs e)
{
    getData(); 
}

private void getData()  //Surrogate for your getData method
{
    MessageBox.Show("Getting Data");
}

Upvotes: 1

Adam
Adam

Reputation: 3665

I think your problem is you are creating a new instance of Form1 instead of passing in a reference to the original Form1. If you pass in a reference to the original Form1; you will be updating that instances. Not a new one.

See this post for similar problem to what I think your experiencing.

refreshing treeview component from other form

Upvotes: 0

Related Questions