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
AlanPosted Jul 25, 2008, 4:18 PM
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
}
John BeemanPosted Jul 25, 2008, 5:15 PM
Satish KathiPosted Jul 25, 2008, 3:35 PM
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();
.......................
}