C# .NET - how to refresh the form1 in from2 button click event?

Asked By suresh on 31-Aug-12 08:14 AM
how to refresh form1 from form2 button click event
Tom Wilson replied to suresh on 31-Aug-12 01:30 PM
The controls on form 2 that are used to refresh form 1 must have their modifier set to Public, so form 1 can see the controls.  This includes the button of form 2.  Then add a handler for form 2's button click even in form 1.  Form 2 must be declared at the class level in form 1.  This is the code for form 1.  When the button click event on form 2 fires, it changes the text property of a textbox on form 1.

public partial class Form1 : Form
   {
     public Form1()
     {
       InitializeComponent();
        
     }
 
     Form2 frm;
 
     private void dostuff()
     {
       frm = new Form2();
       frm.button1.Click += new EventHandler(button1_Click);
       frm.Show();
     }
 
     private void button1_Click(object sender, EventArgs e)
     {
       this.textBox1.Text = frm.textBox1.Text;
        
     }
 
     private void Form1_Load(object sender, EventArgs e)
     {
       dostuff();
     }
   }
Rohan Dave replied to suresh on 31-Aug-12 03:15 PM
In button click event of your form 2 write below code..

Form1 objFrm = new Form1();
objFrm.Refresh();
Tom Wilson replied to Rohan Dave on 31-Aug-12 09:29 PM
Rohan Dave, the code you posted will create a new instance of Form 1.
suresh replied to Tom Wilson on 01-Sep-12 01:13 AM
hi, Tom, thanks for replying me. it showing me error near
private void dostuff()
   {
     frm = new Form2();
     frm.button1.Click += new EventHandler(button1_Click);
     frm.Show();
   }
button1 control is in form2 or form1 ?
Tom Wilson replied to suresh on 01-Sep-12 07:20 AM
Button1 is on Form 2.  The modifier for Button1 must be change from Private to Public.  You can change the modifier in the Button1 properties window.