0

I need to change the text of a label on a C# Winform that is already open. The form was opened from a different method in my program so it is already open and I don't have access to the original reference of the form when it was first created.

I tried to do this in the code below, but I can not access the label on the form. Is there a way I can change the label on a form (from another method) that is already running?

//http://stackoverflow.com/questions/3861602/c-sharp-how-check-if-the-form-is-already-open-and-close-it-if-it-does
Form fc = Application.OpenForms["form1"];

if (fc != null)
{
     //This does not work.  I can not access the lblNewItems label.
     //The label has it's public modifier set to Public and I am able
     //to set this label successfully when I create the form originally
     //from the other method.

     fc.lblNewItems.Text = "Change text"; 
}

When compiling the above I get the following error:

Error 4 'System.Windows.Forms.Form' does not contain a definition for 'lblNewItems' and no extension method 'lblNewItems' accepting a first argument of type 'System.Windows.Forms.Form' could be found (are you missing a using directive or an assembly reference?)

Can someone please tell me if this is possible to do and if so; how do I change the label on a form that is already opened from another method?

verdesmarald
  • 11,646
  • 2
  • 44
  • 60
fraXis
  • 3,141
  • 8
  • 44
  • 53
  • is the access modifier for that label in the the designer.cs also "public" ? – Thousand Sep 27 '12 at 16:04
  • @Thousand Yes I have the access modifier for the label set to "public". Please read my code comment in my code above. – fraXis Sep 27 '12 at 16:06
  • "This does not work." Ok, **why?** Does it not compile? Does it run but not change the text? Is there an error or exception and what is its text? Help us help you! – verdesmarald Sep 27 '12 at 16:08
  • @verdesmarald When I compile: Error 4 'System.Windows.Forms.Form' does not contain a definition for 'lblNewItems' and no extension method 'lblNewItems' accepting a first argument of type 'System.Windows.Forms.Form' could be found (are you missing a using directive or an assembly reference?) – fraXis Sep 27 '12 at 16:11
  • @verdesmarald Also in the VS 2010 editor it says "Cannot resolve symbol 'lblNewItems' and has the lblNewItems highlighted as red – fraXis Sep 27 '12 at 16:12
  • Ah, well that's much easier to explain! You need to cast `fc` to the actual type of your `Form`. I'm guessing by the name `"form1"`, that your class is `Form1`, so: `Form1 fc = (Form1)Application.OpenForms["form1"];` should work. – verdesmarald Sep 27 '12 at 16:13

2 Answers2

4

Your problem is that fc is of type Form, while your label lblNewItems is actually on some subclass of Form (I'm guessing your class is Form1 based on the question). You need to cast fc to the actual form type before trying to access its elements:

Form1 fc = (Form1)Application.OpenForms["form1"];    
if (fc != null)
{
     fc.lblNewItems.Text = "Change text"; 
}
verdesmarald
  • 11,646
  • 2
  • 44
  • 60
1

The form was opened from a different method in my program so it is already open and I don't have access to the original reference of the form when it was first created.

Create a public static reference to the form in Program.cs, so you can access it from any method, just check for Null before setting the text of the label.

Stefan P.
  • 9,489
  • 6
  • 29
  • 43
  • Is this acceptable to do (i.e. good programming behavior? I don't create the form in Program.cs; I create it in MainForm.cs so should I create the public static reference there instead? – fraXis Sep 27 '12 at 16:08