开发者

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

开发者 https://www.devze.com 2022-12-16 04:21 出处:网络
w开发者_如何学运维hen i create a control by drag and drop VS automatically generate code like this:

w开发者_如何学运维hen i create a control by drag and drop VS automatically generate code like this:

public System.Windows.Forms.Label label1;

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 :(. So how do you change modify of a control to static ?

sorry, im bad at English :(


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; 
   } 
}


It seems that your actual problem is another one: Updating controls from another thread. This should NOT be accomplished by static controls!

These related questions should solve your problem:

How to update textbox on GUI from another thread in c#

How to update GUI from another thread in C#?


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.


Wayne,

  1. No, you don't want a Control to be static. Explain why you think you do and we can find out what the better alternatives are.

  2. Don't edit in *.Designer.cs files. The tools (Forms/Dataset/... designers) have the right to overwrite everything.

Edit:

You have 2 problems to solve,

  1. Accessing the Control from another class. This should be done by passing an instance-reference to that other class. Something like:
    void Form1_Load(..) { otherObject.Form = this; }

  2. Using the Control form another thread. You can never do so directly, always use Control.Invoke(). Divo lists 2 useful links.


You'll have to move the definition out of the autogenerated designer code, from the file Form.Designer.cs to your code Form.cs.


Perhaps you could create a new class that inherits the control in question, and then apply the singleton pattern to it.

That way you have a global (thread safe) point of access to it.


Here is an example of how to use it:

Label label1 = Application.OpenForms["Form1"].Controls["label1"] as Label;


The best way I found is by doing the opposite as above. Although they did not explain the reasons for not doing such as making a control static being a bad thing; means I need to post this in order to solve your question, as it is a question you want answered and not a question just to be told...why...or dont... with no reasoning behind such and does not answer your question. Please see below.

As a control is made you can find the new private created control code on the button of designer.cs for that form. It should be towards the end but will look like this for instance if you make a button.

" #endregion

    Private Button button1;"

If you simply change this to be as follows...

" #endregion

    public static Button button1; "

You will notice all the control code with a reference to this.button1 inside the designer will be red errored. You may Delete "this." on each and it will be good to go.

To reference in another class make sure you are using the namespace above.

using mainform

then below in your class code you can reference the button as....

mainform.button1.text = "blah";

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号