tags:

views:

69

answers:

2

Hey!

I have 2 forms in my project, form1 and form2. When I click in a button in form1 I run this code:

Form tempform = new Form2();
tempform.Show();

In my code for Form2 I have a label which I now need to change the text. How can I access the label?

I tried:

tempform.label1.value = "new text"

And that didn't work, I even tried to access using the Controls collection but I think I messed that up. Is there any way I can access the label? OR is there any way I can pass a value to that new form and then have that form alter the label text.

Thanks

+2  A: 

Controls have protected access by default. You can change that to public, or you can add a method/property to your form2 class to set the label and call that (latter method is generally preferred to preserve encapsulation and because the designer may want to overwrite your public change.).

Joel Coehoorn
I added this method:public void SetLabelValue(string valor) { label1.Text = valor; }And got this error:'System.Windows.Forms.Form' does not contain a definition for 'SetLabelValue' and no extension method
AntonioCS
Make sure you added the function to the right form, call it in the right way, and that form2 was re-compiled before using it in form1
Joel Coehoorn
+7  A: 

If the label value should only be set once, when the form is created, then use a constructor for Form2 like this:

public Form2(string labelValue)
{
  _labelValue = labelValue;
}

and then call that constructor when you create the form.

Alternatively, if the label changes over the lifetime of the form, make a public property:

public string LabelValue
{
  get { return label1.Text; }
  set { label1.Text = value; }
}

Also I would recommend naming the parameters and/or properties to reflect the meaning of the value, for example "titleText" instead of "labelValue". That way Form2 can decide how it wants to display the information (in the title bar, a label, a textbox, etc), and Form1 doesn't have to worry about that.

Edit: Consume the LabelValue property like this:

Form2 newForm = new Form2(); // Assign object to a Form2 instead of Form
newForm.LabelValue = "new text";
newForm.Show();
CodeMonkey1
I tried doing that and this is what I get:Error 1 'System.Windows.Forms.Form' does not contain a definition for 'LabelValue' and no extension method 'LabelValue' accepting a first argument of type 'System.Windows.Forms.Form' could be found
AntonioCS
You are referencing your Form2 object as a Form. The Form type will not have the new methods defined. You must create a Form2 variable. I added an example to my answer.
CodeMonkey1
If you assign the field value through the constructor argument of Form2, then you can keep your newForm as Form reference.
Thedric Walker
I used the constructor to pass the arguments.I did try the properties and I did as you showed but that didn't work :(
AntonioCS
Oh wait I didn't do as you said. I created a generic form instead of a Form2. Sorry sorry I misreadThanks :))
AntonioCS
But one thing I do find strange. I create the form as: Form tempform = new Form2();But this form as the label I created in the form2, and using the constructor thing you suggested works. Why shouldn't it normally work like this? Why do I have to do Form2 tempform = new Form2()???
AntonioCS
Because when you assign it to a Form, the compiler doesn't know it is a Form2. The compiler will only let you call members of the Form class.
CodeMonkey1