Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Forms form
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 27-Jun-11 9:03am
Edited 27-Jun-11 19:54pm
v2
Comments
Uday P.Singh at 27-Jun-11 14:08pm
   
what do you want to do with this code? where is your refresh method?
Uday P.Singh at 27-Jun-11 14:21pm
   
but where are you using delegates?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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)
   {
   ...
   }
  Permalink  
Comments
Member 8015046 at 27-Jun-11 14:40pm
   
THAT IS COOL! Thanks so much!
OriginalGriff at 27-Jun-11 14:44pm
   
You're welcome!
Ramalinga Koushik at 28-Jun-11 0:54am
   
Good Stuff OriginalGriff.
Ankur\m/ at 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:
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
v2
Comments
OriginalGriff at 27-Jun-11 14:44pm
   
Sorry about that! Have a 5 anyway... :laugh:
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You can call a method on from1 when from2 closes like this:
 
in Form1 write this:
 
public void MyrefeshMethod()
        {
            MessageBox.Show("refreshed");
        }
 
and in the Form2 closed event write this:
 
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            Form1 frm = new Form1();
           
            frm.MyrefeshMethod();
 
        }
 
hope this helps Smile | :)
 
for further queries comment here!!
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,382


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 28 Jun 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100