How to pass controls from one form to another using C#
Posted: Sun Feb 07, 2010 2:24 am
There are several methods. The easiest is to write a public property on Form2 to pass the string of the test box as in following example.
Now add the following code to Form1. Then you are able to access the controls of Form2 by using above method.
Code: Select all
public string MyTestBox {
get {return textbox1.Text;}
set {textbox1.Text = value;}
}
Now add the following code to Form1. Then you are able to access the controls of Form2 by using above method.
Code: Select all
Form2 form2;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
form2 = new Form2();
form2.Show();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(form2.MyTestBox);
}