I have a label called LabelX1. This is on form2. On form1, i have a button. I want the button\'s text to be transferred to the other form\'s label. I have tried
<
You need to expose your label or its property.
In form 2:
public string LabelText
{
get
{
return this.labelX1.Text;
}
set
{
this.labelX1.Text = value;
}
}
Then you can do:
form2 frm2 = new form2();
frm2.LabelText = this.button1.text;
Generally the controllers are private. That is why you unable to access it from another form. Above mentioned ways such as passing data through parameters etc are so correct. There is one another method, Go to your form Form2.Designer.cs
private System.Windows.Forms.Label labelX1;
Change 'private' into 'public'. Now the labelX1 is visible to outside.
Or you can do this >>
((Label)frm2.Controls["labelX1"]).Text = "test";
You can make labelX1
public, and it will work, but there is a better way to do this:
http://www.codeproject.com/Articles/14122/Passing-Data-Between-Forms
I changed my parent window property to the following code:
this.MdiParent.Controls["label1"].Text = "test";
Do you have exception? You can make public property on form2, with setter to set text on the label, or make labex1 access modifier public and set it directly. It should work.