Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
I've created a form using C# form application.
 
How to clear the controls like radio buttons, combo boxes along with text boxes when pressing the RESET button.
Posted 25-Dec-11 4:28am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Radio buttons should ALWAYS have at least one in a given group checked. Otherwise, the data is invalid.
 
To answer your question, just enumerate your controls and clear the contents.
 
private void ClearControls(Control ctrl)
{
    if (ctrl.IsContainer)
    {
        foreach (Control child in ctrl.Children)
        {
            ClearControls(child);
        }
    }
    else
    {
        if (ctrl is TextBox)
        {
            ((TextBox)ctrl).Text = "";
        }
        else if (ctrl is CheckBox)
        {
            ((CheckBox)ctrl).Checked = false;
        }
        else if (ctrl is RadioButton)
        {
            ((RadioButton)ctrl).Checked = false;
        }
    }
}
  Permalink  
v2
Comments
Dalek Dave at 25-Dec-11 9:55am
   
5 from me John, that's how I would do it.
(And good point about the Radio Button).
SAKryukov at 25-Dec-11 12:58pm
   
Good points, my 5, credited in my answer.
--SA
theanil at 25-Dec-11 13:14pm
   
My 5+
nischalinn at 25-Dec-11 21:37pm
   
Thanks for the solution.
Will you please explain me the code:
private void ClearControls(Control ctrl)
{
if (ctrl.IsContainer)
{
foreach (Control child in ctrl.Children)
{
ClearControls(child);
}
}
   
The method is recursive and accepts any control. When it encounters a container control (like a GroupBox, or a Panel), the method calls itself and the child controls within the container control are enumerated. This allows you to enumerate ALL controls on the form with a single method. Google "recursion" for more complete descriptions of the paradigm). By the way, if I provided the correct answer, make sure you "accept" the solution (and you can accept more than one if it's appropriate).
Espen Harlinn at 31-Dec-11 8:27am
   
Good reply, John :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

There is no a strict sense to be attributed to the term "clear" when it comes to a whole set of all controls. It makes no general sense because controls are not necessarily independent, at least one radio button in a group, as John correctly pointed out, should be checked, etc.
 
But in many cases, you really need some method of putting all the controls in some initial state. Normally, you call it before a form is shown. If you want to call it again, you can also call it in your "Reset" buttons. (This looks a bit strange though; the usual name for it is called "Defaults" or something like that.) In most cases this is not needed, but I presume you have some reasons to do it.
 
I just want to point out, that in any more or less complex application such function should be semantic and specific to your application. Here is why: you usually need to separate your UI from a data layer. In this case, you have some data model which populates UI and is modified when a user edits anything (just modifies) using the UI. This way, you have a uniform throughout the application, semantic methods of population and data update. In this approach, the setting up of initial UI state is no different: you have an initial-state instance of data and your "reset" is the population of the UI from this initial instance of the data model.
 
If you follow this simple approach, you can avoid doing the same thing in different ways and thus avoid many mistakes. After all, always remember one of the most important principles: Don't Repeat Yourself (DRY), see http://en.wikipedia.org/wiki/Don%27t_repeat_yourself[^].
 
—SA
  Permalink  
Comments
idle63 at 25-Dec-11 13:16pm
   
Seems this SA has always good background information ;)
5 from a cp noob for this and also for s1
Regards.
SAKryukov at 25-Dec-11 13:46pm
   
Thank you very much.
--SA
nischalinn at 25-Dec-11 19:49pm
   
Thanks for the information. I am happy to learn about DRY.
Can I have the code for my question from your viewpoint, I mean using DRY approach?
SAKryukov at 31-Dec-11 12:10pm
   
DRY is not "approach", this is a principle which filters out some approaches as bad, not maintainable.
The approach is having a data model. Let's say, in a simplest case, you create a pure-data class; and its instance is represented by you UI; you have method DataToUi (call it before showing and on your "reset") and UiToData (to update data as edited)...
--SA
Espen Harlinn at 31-Dec-11 8:28am
   
Good reply, Sergey!
SAKryukov at 31-Dec-11 12:10pm
   
Thank you, Espen.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 230
1 OriginalGriff 202
2 Richard MacCutchan 185
3 Tomas Takac 146
4 CPallini 125
0 OriginalGriff 5,130
1 DamithSL 4,237
2 Maciej Los 3,700
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,846


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 25 Dec 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100