C# Updating Form1 when closing Form2

refresh form c#
refresh form after closing another form c#
refresh the parent form on closing the child form in c# windows application
c# reload form without closing
update one form from another c#
update datagridview from another form c#
access vba refresh form from another form
refresh datagridview c#

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?

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");
}

Update. Form(1) opens another form(2). When Form(2) closes, Form , Learning about C#, delegates and events. I have made a windows Form(1) that has a button that opens up another form(2). I want to be able to  Hope these code snippets has helped you to close one form and open another form in C# or VB.NET.Leave your comments in the comment section below. Related Items : Close one form and Open another form On Button click, Open new Form but Closing current Form, vb.net close form from another form, How to close form1 and open form2 in VB.NET,

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

C# Updating Form1 when closing Form2 - .net - iOS, C# Updating Form1 when closing Form2 - .net. to frm happens in a new window. How do I access the existing Form1 window's getData() method from Form2? I have two forms- form1 and form2. I call form2 from form1 using the below snippet: Application.run(new Form2()); Form2 f2=new Form2(); f2.show(); This code is working absolutely fine and i could see the form2 loading from form1. Now i need to repeat this for say 5 times.

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();
        }

Form.Closed Event (System.Windows.Forms), When a button is clicked in form1, form 2 is opened, keeping form1 also in opened state. now after the changes made in form2, if we close  Now, I have two forms, called form1 and form2, in the form1 there's a button, when I click it, then open the form2. Question: in the form2, I want to create a button when I click it, the form2 close and the form1 close.

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

Refresh Main form after closing sub form, C# Updating Form1 when closing Form2. refresh form c# refresh the parent form on closing the child form in c# windows application refresh form after closing  showConnectionStatus function in Form1 will call to a function in Form2 to update information in Textboxes. configElement is an string array with 4 elements: public void showConnectionStatus() { Program.form2.updateSMOStatus(configElement, configElement, configElement + "," + configElement); }

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
}

C# Updating Form1 when closing Form2, then if the form2 closes it will refresh the form1 but because the form1 Refresh​(); //put any code here that you want to run after form 2 is closed. Close. This video is unavailable. Watch Queue Queue. Watch Queue Queue. Remove all; How to Use control in form2 to form1 in c# - Duration: 4:23. Hamzah Al-washali 49,617 views.

refresh form from another form [SOLVED], How to Close Form1 and Open Form2 in C#-Many ways you can close current 1) Close Form1 and Show Form2. Then Update the Main() method as below. c# Auto-Refresh DataGridView in Form1 after inserting in Form2 Thanks for watching !

How to Close Form1 and Open Form2 in C#, How do I enter data into a form, and then close it and display it in my first form? C#, WinForms; Two Forms, Form1 and Form2; Form1 starts with the If you're trying to create a new instance of a Form in order to update it,  That would be cool if in form2 something was happening in form2 or another class outside the form1 but if you call form2 from form1 and want to change something in form1 when form2 closing well because forms only update on event for the GUI you would need to point to the form1 function you want triggered when closing form2. You need a delegate.

Passing data between two Forms in WinForms, how to refresh parent window when child window is closed c#? refresh parent window from Duration: 27:39 Posted: Apr 12, 2017 The thing is that after I close Form2 the ContectMenuStrip in Form1 needs to be updated or refreshed so that the new items pops up in the menu. So I figure if there is a way to update/refresh ContenctMenuStrip in Form1 from Form2 just before closing it. How can this be done?

Comments
  • How are you creating and showing Form2 in Form1?
  • While opening Form2 try pass reference of treeview by ref keyword in contructor of Form2 like ` public Form2(ref TreeView tv) `, after evry operation on Form2 update treeView also and refresh Form1() on Form2() closing, Hope u understand.