如何在 Visual Studio 中将控件的修饰符更改为 Static

发布于 2024-08-17 14:46:20 字数 697 浏览 3 评论 0原文

当我通过拖放创建控件时,VS会自动生成如下代码:

public System.Windows.Forms.Label label1;

当我想将该控件的修饰符更改为 Static 时,我转到 Form1.Designer.cs 并编辑为:

public static System.Windows.Forms.Label label1;

没关系。但是当我修改每个控件时,VS会自动将其更改为原点:(。 那么如何将控件的修改更改为静态呢?

抱歉,我英语不好:(


评论中的代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

半城柳色半声笛 2024-08-24 14:46:21

您必须将定义从文件中的自动生成的设计器代码中移出
Form.Designer.cs 到您的代码 Form.cs

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

穿透光 2024-08-24 14:46:21

也许您可以创建一个继承相关控件的新类,然后对其应用单例模式。

这样你就有了一个全局(线程安全)的访问点。

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.

铃予 2024-08-24 14:46:21

以下是如何使用它的示例:

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

Here is an example of how to use it:

Label label1 = Application.OpenForms["Form1"].Controls["label1"] as Label;
阿楠 2024-08-24 14:46:21

我发现最好的方法是做与上面相反的事情。尽管他们没有解释不这样做的原因,例如使控件静态是一件坏事;意味着我需要发布此内容才能解决您的问题,因为这是您想要回答的问题,而不是只是被告知的问题...为什么...或不...背后没有任何推理并且不回答你的问题。请看下文。

创建控件后,您可以在该表单的 Designer.cs 按钮上找到新的私有创建的控件代码。它应该在最后,但如果你制作一个按钮,它看起来会像这样。

" #endregion

    Private Button button1;"

如果您简单地将其更改为如下...

" #endregion

    public static Button button1; "

您会注意到设计器内引用 this.button1 的所有控制代码都将出现红色错误。您可以删除“这个”。每一个都很好。

要在另一个类中引用,请确保您使用上面的命名空间。

使用 mainform

然后在您的类代码中,您可以将按钮引用为...

mainform.button1.text = "blah";

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

饮惑 2024-08-24 14:46:20

看来您的实际问题是另一个问题:从另一个线程更新控件。这不应该通过静态控件来完成!

这些相关问题应该可以解决您的问题:

如何更新文本框在 c# 中的另一个线程的 GUI 上

如何从另一个线程更新 GUI C#?

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#?

愚人国度 2024-08-24 14:46:20

设计器代码不应该由用户修改,因为每次您在设计器中更改表单时,Visual Studio 都会重新编写它(正如您所发现的)。

一种方法是将控件声明和初始化移至非设计器代码文件。但是,这意味着您的控件将不再出现在设计器中。

编辑:
这不是让其他线程可以访问您的控件的方法!我想不出使控件静态的有效理由。

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.

和影子一齐双人舞 2024-08-24 14:46:20

Wayne,

  1. ,您不希望控件是静态的。解释一下您认为这样做的原因,我们可以找出更好的替代方案。

  2. 不要在 *.Designer.cs 文件中进行编辑。工具(表单/数据集/...设计者)有权覆盖所有内容。

编辑:

您有 2 个问题需要解决,

  1. 从另一个类访问控件。这应该通过将实例引用传递给其他类来完成。类似于:
    void Form1_Load(..) { otherObject.Form = this; }

  2. 使用另一个线程的控件。您永远不能直接这样做,始终使用 Control.Invoke()。 Divo 列出了 2 个有用的链接。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文