This is how you pass data from one form to another in c#. There are lot of methods to do this and this one is the easiest of the lot.
USING CONSTRUCTOR
In Form1 in the function where you move to the other form pass the value you want to send to the other form while instantiating the Form Object.
private void button1_Click(object sender, System.EventArgs e)
{
Form2 frm=new Form2(textBox1.Text);
frm.Show();
}
In Form2 add a paramaterized constructor to receive that data that was sent from Form1.
public Form2(string strTextBox)
{
InitializeComponent();
label1.Text=strTextBox;
}
And that is it you have passed data from one form to another.
USING CONSTRUCTOR
In Form1 in the function where you move to the other form pass the value you want to send to the other form while instantiating the Form Object.
private void button1_Click(object sender, System.EventArgs e)
{
Form2 frm=new Form2(textBox1.Text);
frm.Show();
}
In Form2 add a paramaterized constructor to receive that data that was sent from Form1.
public Form2(string strTextBox)
{
InitializeComponent();
label1.Text=strTextBox;
}
And that is it you have passed data from one form to another.
No comments:
Post a Comment