I am having trouble changing a label on a different form. Can anyone tell me what I'm doing wrong here? Thanks.
On Form 1, I have the following, where label1 is defined in Form1.Designer:
public string Lbl
{
get {
return label1.Text;
}
set {
label1.Text = value;
MessageBox.Show(label1.Text); //Debug: Value here shows correctly but label does not actually change on Form
}
}
On Form2 I have this:
void setLabelInForm1()
{
Form1 frm1 = new Form1();
frm1.Lbl = "LOADED";
}
private void button1_Click_1(object sender, EventArgs e)
{
........................
setLabelInForm1();
.......................
}
Loading

AlanRank #669 · 3.6K reputationPosted Jul 25, 2008, 12:18 PM EDT
A further thought here.
If Form1 is the start up form, then you don't want to create a new instance - you want to access the existing instance:
void setLabelInForm1()
{
Form1 frm1 = (Form1)Application.OpenForms["Form1"];
frm1.Lbl = "LOADED";
frm1.Visible = true; // if necessary
}
Satish KathiRank #8,369 · 223 reputationPosted Jul 25, 2008, 11:35 AM EDT
You are not displaying the Form1 hence you are not seeing the value. Just add frm1.Show(); after assigning the value to the Label. See the code below in bold font
public string Lbl
{
get {
return label1.Text;
}
set {
label1.Text = value;
MessageBox.Show(label1.Text); //Debug: Value here shows correctly but label does not actually change on Form
}
}
On Form2 I have this:
void setLabelInForm1()
{
Form1 frm1 = new Form1();
frm1.Lbl = "LOADED";
frm1.Show();
}
private void button1_Click_1(object sender, EventArgs e)
{
........................
setLabelInForm1();
.......................
}
John Beeman15 reputationPosted Jul 25, 2008, 1:15 PM EDT