Click here to Skip to main content
15,885,673 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

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.

C#
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;
        }
    }
}
 
Share this answer
 
v2
Comments
Dalek Dave 25-Dec-11 9:55am    
5 from me John, that's how I would do it.
(And good point about the Radio Button).
Sergey Alexandrovich Kryukov 25-Dec-11 12:58pm    
Good points, my 5, credited in my answer.
--SA
theanil 25-Dec-11 13:14pm    
My 5+
nischalinn 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);
}
}
#realJSOP 26-Dec-11 6:21am    
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).
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
 
Share this answer
 
Comments
[no name] 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.
Sergey Alexandrovich Kryukov 25-Dec-11 13:46pm    
Thank you very much.
--SA
nischalinn 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?
Sergey Alexandrovich Kryukov 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 31-Dec-11 8:28am    
Good reply, Sergey!

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900