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
If the label value should only be set once, when the form is created, then use a constructor for Form2 like this:
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:
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: