0

Possible Duplicate:
Best way to access a control on another form in WinForms?

I know this is a blinding obvious question but I am a c# newbie and find it a little confusing, so any help would be greatly appreciated...

I have a mainform and a second form, let's call it form2. form2 has some labels on it. I would like to control the text of those labels from the mainform class / cs sheet. No matter what I try, I cannot seem to access them. I have set some of them to public already and I still can't "see" them in the mainform class.

mainform is wpf (as is the project). form2 is a regular winform.

If someone would help me out I would be eternally grateful.

Thanks,

Community
  • 1
  • 1
sotiris
  • 63
  • 1
  • 1
  • 8
  • 2
    http://stackoverflow.com/questions/8566/best-way-to-access-a-control-on-another-form-in-winforms – Uwe Keim Jan 12 '12 at 10:40
  • Thanks Uwe, I already that and didn't understand it. – sotiris Jan 12 '12 at 10:47
  • If you didn't understand ["create a property"](http://stackoverflow.com/a/8573/366904), it's time to get a book on C# and learn the language... – Cody Gray - on strike Jan 12 '12 at 10:53
  • 1
    I already have a book on C# and am reading through it at the moment, but I thought I would ask some of the more experienced members of stack overflow for this particular issue - I have found people on here in the past to be friendly, helpful and informative. Thanks for the suggestion anyway, Cody Gray, and good luck with whatever it is you do in life. – sotiris Jan 12 '12 at 11:07

1 Answers1

9

As yours labels are private, they can be accessed from the owner form only. Do not try to change them to public, it's a wrong approach (public members are evil).

Add a public method that updates your labels so it can be accessed from your second form.

form2:

public void SetTextForLabel(string myText)
{
    this.myLabel.Text = myText;
}

mainform:

myForm2Instance.SetTextForLabel("my text");
ken2k
  • 48,145
  • 10
  • 116
  • 176
  • Thanks ken, when I put the code you suggested in the mainform, I get a wavy red line under "SetTextForLabel" and the following error: Error 1 'System.Windows.Forms.Form' does not contain a definition for 'SetTextForLabel' and no extension method 'SetTextForLabel' accepting a first argument of type 'System.Windows.Forms.Form' could be found (are you missing a using directive or an assembly reference?) – sotiris Jan 12 '12 at 10:55
  • The error message means you are trying to use an instance of the `Form` type, not your own `Form2` type. Post the few lines around your error so we can help... – ken2k Jan 12 '12 at 11:04
  • Thanks Ken. At the top of the class I declare the form - public static Form WaitFormV; --- then in a progress changed event further down the class I have --- WaitFormV = new DBWaitForm(); WaitFormV.ShowDialog(); WaitFormV.SetTextForLabel("my text"); – sotiris Jan 12 '12 at 11:13
  • Hey, I managed to figure it out for myself. Thanks, Ken. I was declaring the form incorrectly. Cheers for all your help. – sotiris Jan 12 '12 at 11:33
  • Haha I like how you say "public members are evil", then define a public method later XD – Liam McInroy Apr 29 '12 at 00:20