C# 将复选框设置为静态

发布于 2024-11-09 15:00:13 字数 420 浏览 0 评论 0原文

我有一个为 Windows 窗体创建的复选框,如何将其设置为静态?

public static CheckBox checkthis;

此代码创建一个新的静态的,我想要做的是将我在设计器中创建的一个设置为静态。

更新:

我尝试了下面的答案,它起作用了,尽管复选框从表单中消失了,并且出现了各种其他问题。相反,我这样做了创建一个新的并执行了以下操作:

public static CheckBox checkthisnew;

...
checkthisnew = checkthis;

无论哪种方式,我现在都意识到我失败了,我只是可以使用事件列表上更改的状态,所以一切都很好...

很抱歉没有让我背后的推理更清楚,但我非常感谢您的回答,谢谢。

I have a checkbox that I created to windows forms, how can I set it to static?

public static CheckBox checkthis;

This code creates a new one as static, what I want to do is set one that I have created in the designer to static.

Update:

I tried below answer and it worked, though the checkbox disappeared from the form and various other issues kicked in. Instead I did this create a new one and did this:

public static CheckBox checkthisnew;

...
checkthisnew = checkthis;

Either way, I have now realised that I am fail and that I just can use the state changed on the events list, so all is well...

Sorry for not making my reasoning behind this more clear, I do appreciate your answers though, thank you.

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

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

发布评论

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

评论(3

伪装你 2024-11-16 15:00:13

编辑 MyForm.Designer.cs 文件,就在复选框声明所在的位置。请注意,如果您使用设计器再次修改 UI,您的更改将被还原,因此您必须再次执行此操作。

Edit the MyForm.Designer.cs file, right where the declaration for your checkbox is. Note that your changes will be reverted if you use the designer to modify the UI again so you'll have to do this again.

神妖 2024-11-16 15:00:13

将依赖的 UI 控件设为静态不会带来任何好处。将组件设为静态是一回事,这可能没问题,但对于像 CheckBox 这样的东西,你只是自找麻烦。对于初学者来说,单个控件只能有一个父控件。因此,您不能只创建控件的单个实例,并期望能够将其添加到多个表单中,并且所有内容都会神奇地同步。如果您需要共享某些值,请以正确的方式进行操作并绑定到它们,注册一些事件,共享而不是保存该值的控件,或其他类似的方法。

我也不建议您修改生成的文件(特别是如果它是从您一直使用的工具生成的)。如果您必须坚持将控件设为静态,请在类的源文件中声明它,而不是设计器生成的文件,类被声明为部分是有原因的。

您可能正在尝试共享 CheckBox 代表的一些 bool 值。将其设为静态属性。

public partial class MyForm : Form
{
    public static bool IsToggled { get; set; }
}

如果您想将其与事件或其他什么相关联,您可以随时更改访问器的实现。

Nothing good can come from making a dependent UI control static. It's one thing to make a component static and that could be ok, but for something like a CheckBox, you're just asking for trouble. For starters, a single control can only have one parent. So you can't just make a single instance of your control and expect to be able to add it to multiple forms and everything will magically appear to be in sync. If you need to share some values, do it the right way and bind to them, register some events, share the value and not the control that holds the value, or other similar methods.

I also cannot recommend you modify generated files (especially if it's generated from a tool you're using all the time). If you must insist on making the control static, declare it in your source file for the class, not the designer-generated file, the classes are declared partial for a reason.

You're probably trying to share some bool value that the CheckBox represents. Make that a static property.

public partial class MyForm : Form
{
    public static bool IsToggled { get; set; }
}

If somewhere down the line you want to tie that to an event or whatever, you could always change the implementation of the accessors.

情何以堪。 2024-11-16 15:00:13

嗯...没有更多上下文,我建议为此使用公共布尔属性。
让 UI 项目保持静态并不是一个好主意。

尝试这样的事情:

public class MyForm : Form
{
    public static bool IsWhateverSet {get; private set;}
}

Hmm... without more context, I'd suggest to use a public boolean property for this.
Having a UI item be static is not a good idea.

Try something like this:

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