Reputation: 39
Hi I'm new to C# and I want to display the value of my textBox1 in form1 to my Label1 in form2. I tried using this:
private void button1_Click(object sender, EventArgs e)
{
label1.Text = textBox1.Text;
Form2 frm = new Form2();
frm.Show();
this.Hide();
}
But it didn't work because it's in another form. Can someone tell me how to do it right?
Upvotes: 1
Views: 10609
Reputation: 1826
Another option
In form1
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.LabelText = "My Text";
from.ShowDialog();
}
In Form 2
private string labelText;
public string LabelText { get { return labelText; } set { labelText = value; } }
private void Form2_Load(object sender, EventArgs e)
{
label.Text = LabelText;
}
Upvotes: 0
Reputation: 16
Try to do this :
// In Form1.cs.
private Form2 otherForm = new Form2();
private void GetOtherFormTextBox()
{
textBox1.Text = otherForm.label1.Text;
}
private void button1_Click(object sender, EventArgs e)
GetOtherFormTextBox();
}
Upvotes: 0
Reputation: 539
In form1 write this code
private void button1_Click(object sender, EventArgs e)
{
Form2 form = new Form2(TextBox1.Text);
form.Show();
}
In form2 write this
public Form2(string text)
{
InitializeComponent();
label1.Text = text;
}
Upvotes: 2