简体   繁体   English

C#:重置面板中的控件(WinForms)

[英]C#: Reset Controls in a Panel (WinForms)

What is a quick way to reset all the Controls inside a panel to their initial states (Compile-time state)? 将面板中的所有Controls重置为其初始状态(编译时状态)的快速方法是什么? I have TextBoxes , RadioButtons , ComboBoxes , and CheckBoxes to reset. 我有要重置的TextBoxesRadioButtonsComboBoxesCheckBoxes I'd like them to reset to the values when the program first ran. 我希望他们在程序首次运行时将其重置为值。 I'd like a solution that does not involve looping or recursion mainly because I don't want to reimplement the same thing over again when I start witha new project. 我想要一个不涉及循环或递归的解决方案,主要是因为当我开始一个新项目时,我不想再次重新实现同一件事。 I'm simply finding a set of methods to call that will do the job. 我只是在寻找一组可以完成工作的方法。 Are there any? 有吗

Your controls have no compile time state, because state is a runtime concept. 您的控件没有编译时状态,因为状态是运行时概念。

I think you mean you want controls re-initialized to the state as shown on your property sheets. 我认为您的意思是希望将控件重新初始化为属性表中所示的状态。 This state is applied by the generated code located in InitializeComponent , so to re-apply that state, you could just call it again. 此状态由InitializeComponent生成的代码应用,因此要重新应用该状态,您可以再次调用它。

The only problem is InitializeComponent also wires up events, and you probably don't want to do that twice. 唯一的问题是InitializeComponent也连接事件,您可能不想重复两次。 You could possibly work around this by deregistering all of your events before calling it, or by deduplicating the invocation list afterward (see this answer ). 您可以通过以下方法解决此问题:在调用之前取消所有事件的注册,或者之后取消重复的调用列表(请参见此答案 )。

I don't recommend any of this. 我不推荐任何一个。 The best approach would be to write your own method that sets the properties the way you want them, one by one. 最好的方法是编写自己的方法,以一种想要的方式逐一设置属性。 Sometimes ya gotta write code. 有时您必须编写代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM