C # pass value to the child form of the Winform parent form,
This blog reposted: http://www.cnblogs.com/freeliver54/archive/2009/02/11/1388173.html
The results of this example are as follows:
Form1 is the parent form (including textBox1 and button1)
Form2 is a child form (including textBox2 and button2)
The parent form transmits values to the child form.
============================
1. Click Form1's button1 to open Form2
You can call the constructor of the overloaded sub-form to directly input values to the sub-form.
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
Private void button#click (object sender, EventArgs e)
{
Form2 frm2 = new Form2 (this. textBox1.Text );
Frm2.Show ();
}
}
Public partial class Form2: Form
{
Public Form2 ()
{
InitializeComponent ();
}
Public Form2 (string strTextBox1Text)
{
InitializeComponent ();
This. textBox2.Text = strTextBox1Text;
}
}
2. Click Form1's button1 to open Form2
Call the public attribute or method of the subform Form2 to set the value of textBox1 of Form1 to textBox2 of Form2.
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
Private void button#click (object sender, EventArgs e)
{
Form2 frm2 = new Form2 ();
Frm2.TextBox2Text = this. textBox1.Text;
Frm2.Show ();
}
}
Public partial class Form2: Form
{
Public Form2 ()
{
InitializeComponent ();
}
Public string TextBox2Text
{
Set {this. textBox2.Text = value ;}
Get {return this. textBox2.Text ;}
}
}
3. Click Form1's button1 to open Form2
Call the public attribute or method of the parent form Form1 in Form2_Load to set the value of textBox1 of Form1 to textBox2 of Form2.
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
Public string TextBox1Text
{
Set {this. textBox1.Text = value ;}
Get {return this. textBox1.Text ;}
}
Private void button#click (object sender, EventArgs e)
{
Form2 frm2 = new Form2 ();
Frm2.Show (this); // or frm2.ShowDialog (this );
/// Or
// Form2 frm2 = new Form2 ();
// Frm2.Owner = this;
// Frm2.Show (); // or frm2.ShowDialog ();
}
}
Public partial class Form2: Form
{
Public Form2 ()
{
InitializeComponent ();
}
Private void Form2_Load (object sender, EventArgs e)
{
Form1 frm1 = (Form1) this. Owner;
This. textBox2.Text = frm1.TextBox1Text;
}
}
The child form transmits a value to the parent form.
============================
4. Click Form1's button1 to open Form2
Click "button2" of "Form2 ".
In the button2_Click event, use this. Owner to set the value of textBox2 of Form2 to textBox1 of Form1.
Disable Form2
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
Private void button#click (object sender, EventArgs e)
{
Form2 frm2 = new Form2 ();
Frm2.Show (this); // or frm2.ShowDialog (this );
/// Or
// Form2 frm2 = new Form2 ();
// Frm2.Owner = this;
// Frm2.Show (); // or frm2.ShowDialog ();
}
}
Public partial class Form2: Form
{
Public Form2 ()
{
InitializeComponent ();
}
Private void button2_Click (object sender, EventArgs e)
{
Form1 frm1 = (Form1) this. Owner;
// If textBox1 is placed in panel1, search for panel1 and then textBox1.
(TextBox) frm1.Controls ["textBox1"]). Text = this. textBox2.Text;
This. Close ();
}
}
5. Click Form1's button1 to open Form2
Click "button2" of "Form2 ".
In the button2_Click event, use this. Owner and call the Public attributes or methods of the parent form Form1
Set the value of textBox2 of Form2 to textBox1 of Form1.
Disable Form2
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
Public string TextBox1Text
{
Set {this. textBox1.Text = value ;}
Get {return this. textBox1.Text ;}
}
Private void button#click (object sender, EventArgs e)
{
Form2 frm2 = new Form2 ();
Frm2.Show (this); // or frm2.ShowDialog (this );
/// Or
// Form2 frm2 = new Form2 ();
// Frm2.Owner = this;
// Frm2.Show (); // or frm2.ShowDialog ();
}
}
Public partial class Form2: Form
{
Public Form2 ()
{
InitializeComponent ();
}
Private void button2_Click (object sender, EventArgs e)
{
Form1 frm1 = (Form1) this. Owner;
Frm1.TextBox1Text = this. textBox2.Text;
This. Close ();
}
}