Click here to Skip to main content
15,887,346 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi to all
I have two Windows form : form1 and form2
I want label.text in form1 change when I click in button in form2 .when close form2 I need label.text not change.
I write a public method in form1 but this can't change label.text
How can I do this ????
Posted
Comments
BillWoodruff 16-Sep-11 4:32am    
Does 'form1 create 'form2: if not: in what context (in what Form or UserControl or whatever) are both 'form1 and 'form2 created ? When you say "child Form" do you refer to using MDI Windows, or to simply setting the Parent property of a Form ?

The best way is for Form2 to signal an event which Form1 handles and gets the new data via a public property in form2. That way, Form2 need no nothing about Form1, and Form1 need only get teh data if it is actually interested in it.

In the child form:

C#
public partial class frmChild : Form
   {
   // Signal file change happened
   public event EventHandler Changed;

   protected virtual void OnChanged(EventArgs e)
      {
      EventHandler eh = Changed;
      if (eh != null)
         {
         eh(this, e);
         }
      }

   private void DoSomethingToChangeData()
      {
      OnChanged(null);
      }
   }

----- The asign to eh is in case the handler changes between null check
----- and exec.
----- (unlikely, but possible)

----- The null check is to ensure there is a handler. If not, better to
----- ignore it gracefully, than to rely on the thrown exception
----- (NullReferenceException)

In the Parent form:

C#
public frmParent()
    {
    frmChild.Change += new frmChange.ChangeHandler(Changed);
    }

private void ShowChildForm()
    {
    // Link in his event so if it changes, we detect it.
    frmChild fd = new frmChild();
    fd.ShowDialog();
    }

//
// Fired when the data is changed at a lower level.
//
private void Changed(object sender, EventArgs e)
    {
    Form2 f = sender as Form2;
    if (f != null)
        {
        myLabel.Text = f.Data;
        }
    }
 
Share this answer
 
Comments
esmailian 15-Sep-11 14:01pm    
This Ok if child EventHandler Changed have STATIC . other thing my need label.text get data of textbox in child form. HOW ?
C#
public frmParent()
     {
     frmChild.Change += new frmChange.ChangeHandler(Changed);
     }

 private void ShowChildForm()
     {
     // Link in his event so if it changes, we detect it.
     frmChild fd = new frmChild();
     fd.ShowDialog();
     }

 //
 // Fired when the data is changed at a lower level.
 //
 private void Changed(object sender, EventArgs e)
     {
     Form2 f = sender as Form2;
     if (f != null)
         {
         myLabel.Text = f.Data;
         }
     }
 
Share this answer
 
Comments
fjdiewornncalwe 20-Sep-11 15:06pm    
Why would you copy and paste another user's answer from the same question?

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