Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi I am Computer Science Student. 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 call a refresh method on Form(1) after form(2) closes. I am currently messing with the event AfterClosing on form(2), but I dont know how to call a method on form(1) from form(2).

Any suggestion or any ideas that people can help with?
public partial class Form1 : Form //Windows form
private void button5_Click(object sender, EventArgs e) //Opens up webbrowser form
{
Form2 form2 = new Form2();
form2.Show();
}      

public partial class Form2 : Form //Windows form with web browser
{
   public Form2()
   {
      InitializeComponent();
   }
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
   {
      //Does refresh on form1
   }
}


Thanks in Advance.
Posted
Updated 27-Jun-11 18:54pm
v2
Comments
Uday P.Singh 27-Jun-11 14:08pm    
what do you want to do with this code? where is your refresh method?
Uday P.Singh 27-Jun-11 14:21pm    
but where are you using delegates?
P.Metaxas 11-Dec-18 16:37pm    
Thanks! You saved my project!

I would define the Form2_FormClosing() handler in the Form1 class - that way Form2 doesn't need to know anything about Form1, and Form1 can Refresh() itself. Register the handler on Form1 after you create Form2, typically before you show it.

<Edit>
Argh, OriginalGriff beat me to it!
<\Edit>

Dybs
 
Share this answer
 
v2
Comments
OriginalGriff 27-Jun-11 14:44pm    
Sorry about that! Have a 5 anyway... :laugh:
You can call a method on from1 when from2 closes like this:

in Form1 write this:

C#
public void MyrefeshMethod()
        {
            MessageBox.Show("refreshed");
        }


and in the Form2 closed event write this:

C#
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            Form1 frm = new Form1();
           
            frm.MyrefeshMethod();

        }


hope this helps :)

for further queries comment here!!
 
Share this answer
 
Easy peasy: when you create the Form2 instance, hook into the FormClosing event. You will then automatically get executed, and can refresh whatever you need to. That way, Form2 does not need to know anything about form1.
private void button5_Click(object sender, EventArgs e) //Opens up webbrowser form
   {
   Form2 form2 = new Form2();
   form2.FormClosing += new FormClosingEventHandler(ChildFormClosing);
   form2.Show();
   }
private void ChildFormClosing(object sender, FormClosingEventArgs e)
   {
   ...
   }
 
Share this answer
 
Comments
Member 8015046 27-Jun-11 14:40pm    
THAT IS COOL! Thanks so much!
OriginalGriff 27-Jun-11 14:44pm    
You're welcome!
[no name] 28-Jun-11 0:54am    
Good Stuff OriginalGriff.
Ankur\m/ 28-Jun-11 1:02am    
Why are you updating question(s) that was asked, answered and ACCEPTED 10 hours back?
It brings the question in the active list and annoys answerer. :doh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900