What is a quick way to reset all the Controls
inside a panel to their initial states (Compile-time state)? I have TextBoxes
, RadioButtons
, ComboBoxes
, and CheckBoxes
to reset. 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?
Asked
Active
Viewed 641 times
0

Donovan Keating
- 1,715
- 3
- 17
- 27
-
1What, for example, would be the 'initial state' of a TextBox? Do you simply want to clear the text, or do you want to reset ALL properties of it into some default value? Same goes with other controls? – Sach Aug 02 '17 at 22:34
-
It sounds like you are asking for an abracadabra magic code. – LarsTech Aug 02 '17 at 22:35
-
Winforms? WPF? MVC? Maybe just destroy the form and show a new instance? – Austin T French Aug 02 '17 at 22:37
-
@Sach I edited my post. :) – Donovan Keating Aug 02 '17 at 22:38
-
@LarsTech just trying to find a simpler way, really. – Donovan Keating Aug 02 '17 at 22:38
-
@AustinFrench WinForms. How do I do that? – Donovan Keating Aug 02 '17 at 22:39
-
just create a new form instance? – Lei Yang Aug 03 '17 at 02:09
1 Answers
2
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.
The only problem is InitializeComponent
also wires up events, and you probably don't want to do that twice. 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.

John Wu
- 50,556
- 8
- 44
- 80