C# 将复选框设置为静态
我有一个为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑
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.将依赖的 UI 控件设为静态不会带来任何好处。将组件设为静态是一回事,这可能没问题,但对于像
CheckBox
这样的东西,你只是自找麻烦。对于初学者来说,单个控件只能有一个父控件。因此,您不能只创建控件的单个实例,并期望能够将其添加到多个表单中,并且所有内容都会神奇地同步。如果您需要共享某些值,请以正确的方式进行操作并绑定到它们,注册一些事件,共享值而不是保存该值的控件,或其他类似的方法。我也不建议您修改生成的文件(特别是如果它是从您一直使用的工具生成的)。如果您必须坚持将控件设为静态,请在类的源文件中声明它,而不是设计器生成的文件,类被声明为
部分
是有原因的。您可能正在尝试共享
CheckBox
代表的一些bool
值。将其设为静态属性。如果您想将其与事件或其他什么相关联,您可以随时更改访问器的实现。
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 theCheckBox
represents. Make that a static property.If somewhere down the line you want to tie that to an event or whatever, you could always change the implementation of the accessors.
嗯...没有更多上下文,我建议为此使用公共布尔属性。
让 UI 项目保持静态并不是一个好主意。
尝试这样的事情:
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: