Search code examples
c#modifier

How to change modifier of a control to Static in Visual Studio


When I create a control by drag and drop VS automatically generate a code like this:

public System.Windows.Forms.Label label1;

And when I want to change modifier of that control to Static, I go to Form1.Designer.cs and edit to:

public static System.Windows.Forms.Label label1;

It's ok. But when I modify every control, VS automatically change it to origin.
How can change/modify a control to static ?

code from a comment:

    public static void setLabelInfoVisible(bool visible) 
    { 
       if (Form1.labelInfo.InvokeRequired) 
       { 
          setLabelInfoVisibleDelegate del =
             new setLabelInfoVisibleDelegate(setLabelInfoVisible);
          Form1.labelInfo.Invoke(del, new object[] { visible }); 
       } 
       else 
       { 
         Form1.labelInfo.Visible = visible; 
       } 
    }


Solution

  • Designer code is not supposed to be user modified, as it gets re-written by Visual Studio every time you make changes to your form in the designer (as you have discovered).

    One way forward it to move the control declaration and initialization to the non designer code file. However, that means your control will no longer appear in the designer.

    Edit: This is not the way to make your controls accessible to other threads! I can't think of a valid reason to make the control static.