Click here to Skip to main content
15,173,152 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
how can i pass value from child c# form to the parent form in a windows application
Posted

This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA
   
Depends on how you show your forms.
If you use ShowDialog then it is easy: just create a public property in the child form, and access it directly in the parent:
C#
ChildForm child = new ChildForm();
child.MyProperty = "Hello";
child.ShowDialog();
Console.WriteLine(child.MyProperty);

If you use Show instead then it is slightly harder - you have to create property and an event, or an event and a custom EventArgs

In the child form:
C#
public partial class frmChild : Form
   {
   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#
private void ShowChildForm()
    {
    frmChild fd = new frmChild();
    fd.Changed += new EventHandler(Changed);
    fd.Show();
    }
private void Changed(object sender, EventArgs e)
    {
    frmChild fc = sender as frmChild;
    if (fc != null)
       {
       Console.WriteLine(fc.MyProperty);
       }
    }
   
Comments
AhsanS 24-May-12 7:57am
   
Very nicely answered.
Kuthuparakkal 5-Sep-12 22:38pm
   
nice 5+
Member 14772695 14-Mar-20 6:42am
   
Thank you! It was the easiest way to pass a value from child to parent form. I could manage my child form being closed and to reopen it. Maybe not good to have a public property, but well, that's the easiest solultion
OriginalGriff 14-Mar-20 6:57am
   
That's what public properties are for - it's public fields you have to watch for.
It might be helpful,
Sharing data among Windows Forms[^]

:)
   
Comments
Shahin Khorshidnia 24-May-12 9:47am
   
Thank you for Link
Mohammad A Rahman 24-May-12 9:54am
   
:)
Hi ,
See This Example Will give you idea maybe it useful for you
C#
 //Child Form
public Form2()
{
    InitializeComponent();
}
Label lbl1 = new Label();
Timer tim1 = new Timer();
public Form2(Timer tim , Label lbl)
{
    InitializeComponent();
    tim1 = tim;
    lbl1 = lbl;
}
private void button1_Click(object sender, EventArgs e)
{
    this.Close();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
    lbl1.Text = "Reading from Form2";
    tim1.Start();
}
//Parent Form
public Form1()
{
    InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
    Form2 frm = new Form2(timer1,label1);
    frm.Show();
}

Best Regards
M.Mitwalli
   
v2
Two ways
1) use global variables store values in variables and retrive it in parent form
2) Use getter and setter properties
   

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