1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Send data from parent form to child form

Discussion in 'C#' started by tbreslin, Jul 25, 2008.

  1. tbreslin

    tbreslin New Member

    Joined:
    Jul 25, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I have a project with a Parent Form and Child Form and I want to send data(a field) from the parent form to the child form. Can anyone give me an example of the code to do that?

    Thanks
     
  2. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    Hmm .. :thinking: . This will work :

    Setup :
    (1) Parent and Child are two forms.
    (2) Parent form has a text-box named "txtParentData" where text is entered.
    (3) Child form has a text-box named "txtChildData" where the text from Parent is displayed.
    (4) Parent form has a button named "btnShowChild" that shows the child form.
    (5) Child form has a button named "btnExtractData" which extracts entered text from parent form.

    Code :
    Code:
    // Parent form code :
    
    public partial class Parent : Form
    {
          public Parent()
          {
                InitializeComponent();
          }
          private void btnShowChild_Click(object sender, EventArgs e)
          {
                Child ChForm = new Child(this);          
                ChForm.ShowDialog();
                ChForm.Dispose();
          }
    }
    
    
    
    // Child form code :
    
    public partial class Child : Form
    {
          private Parent PForm;
          public Child(Parent pf)
          {
                InitializeComponent();
                PForm = pf;
          }
          private void btnExtractData_Click(object sender, EventArgs e)
          {
                txtChildData.Text = PForm.txtParentData.Text;
          }
    }
    
     

Share This Page