How do I change a label text in Form2 after clicking a button in Form1?
For example, I want the label text in Form2 to change to "Button 1 was pressed" if I pressed button1 in Form1 and if i pressed button2 in Form1, it will
be "Button 2 was pressed".
Note:
Form1 and Form2 are not shown at the same time. So I would have to click the button and then Form2 will show up with the updated label text.
You can add an event click on button1 on your Form1 class
private void button1_Click(object sender, EventArgs e)
{
Form2 form= new Form2();
form.Show();
// if you want to hide form1
// this.Hide();
form.label1.Text = "Hello World";
}
But before that you should make your label1 marked as public on your Form2.Designer.cs :
public System.Windows.Forms.Label label1;
Refer this :-
C# object of class in different windows form
https://msdn.microsoft.com/en-us/library/system.windows.forms.form(v=vs.110).aspx
How to access form methods and controls from a class in C#?
you have to have a reference to the form object in order to access its elements
the elements have to be declared public in order for another class to access them
Hiding a form and showing another when a button is clicked in a Windows Forms application