Click here to Skip to main content
12,246,406 members (38,886 online)
Rate this:
 
Please Sign up or sign in to vote.
how can i pass value from child c# form to the parent form in a windows application
Posted 24-May-12 1:43am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

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
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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:
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:
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:
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);
       }
    }
  Permalink  
Comments
AhsanS 24-May-12 7:57am
   
Very nicely answered.
Kuthuparakkal 5-Sep-12 22:38pm
   
nice 5+
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

It might be helpful,
Sharing data among Windows Forms[^]

Smile | :)
  Permalink  
Comments
Shahin Khorshidnia 24-May-12 9:47am
   
Thank you for Link
Mohammad A Rahman 24-May-12 9:54am
   
:)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Hi ,
See This Example Will give you idea maybe it useful for you
 //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
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Two ways
1) use global variables store values in variables and retrive it in parent form
2) Use getter and setter properties
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 5 Sep 2012
Copyright © CodeProject, 1999-2016
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