CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2007
    Posts
    116

    Exclamation Passing Value From One Form to another!

    Hi,
    I am trying to pass value from one windows form to another in desktop application using C#.
    I have created two text boxes on Form1 and a button, on button click I am creating Form2 and From2 contains one button, on this button click I have to pass value from Form2 to Form1 textboxes.

    I have created Properties in Form1.

    Code:
    public string Emp
            {
                get
                {
                    return strEmp;
                }
                set
                {
                    strEmp = value;                
                }
            }
    
            public string EmpID
            {
                get
                {
                    return strEmpID;
                }
                set
                {
                    strEmpID = value;
                }
            }

    And on Form1 button click:

    Code:
    private void button1_Click(object sender, EventArgs e)
            {
                Form2 fr = new Form2();
                fr.ShowDialog();
    
                this.textBox1.Text = strName;
                this.textBox2.Text = strID;            
            }

    On Form2:

    Code:
    private void button1_Click(object sender, EventArgs e)
            {
                Form1 fr = new Form1();
    fr. Emp = "Alex";
                fr. EmpID = "1121";
                this.Close();            
            }
    This code is not working, Please let me know where I am wrong.

    Thanks,
    Last edited by HanneSThEGreaT; March 31st, 2010 at 05:59 AM.

  2. #2
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: Passing Value From One Form to another!

    In form2, you are creating a new instance of form1. That's not the same form that opened form2.

    If you want to use properties, move the properties from form1 to 2.
    Then
    Code:
    //form2
    private void button1_Click(object sender, EventArgs e)
    {
      this.Emp = "Alex";
      this.EmpID = "1121";
      this.Close(); 
    }
    
    //form1
    private void button1_Click(object sender, EventArgs e)
    {
      Form2 fr = new Form2();
      fr.ShowDialog();
    
      this.textBox1.Text = fr.Emp;
      this.textBox2.Text = fr.EmpID; 
    }

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Passing Value From One Form to another!

    Frank100, please make use of [CODE] tags when posting code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured