C#将复选框设置为静态 [英] C# Set Checkbox to Static

查看:139
本文介绍了C#将复选框设置为静态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

更新:

我试过下面的答案和它的工作,从形式和各种其他问题踢。相反,我这样创建一个新的,并且这样做:

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.

推荐答案

没有什么可以从一个依赖的UI控件静态。使一个组件静态并且可以确定是一件事,但对于像 CheckBox 这样的东西,你只是要求麻烦。对于初学者,单个控件只能有一个父级。所以你不能只是做一个控件的实例,并希望能够将其添加到多个表单,一切将神奇似乎是同步的。如果您需要共享某些值,请执行正确的方式并绑定到它们,注册一些事件,共享,而不是保存该值的控件或其他类似的方法。

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.

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

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.

您可能试图分享一些 bool c> CheckBox 表示。将 设为静态属性。

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.

这篇关于C#将复选框设置为静态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆