Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I need a code in c# to reset checkboxes and textboxes when a reset button is clicked pls help guys.
Posted

We can't provide detail - we don't know your application, and how it is configured.
There are some things you can do:
C#
Checkbox.Checked = false;

will reset an individual checkbox state.
C#
TextBox.Text = "";
Will reset an individual Textbox.

You can loop through the Controls array:
C#
foreach (Control c in Controls)
   {
   if (c is CheckBox)
      {
      ((CheckBox) c).Checked = false;
      }
   else if (c is TextBox)
      {
      ((TextBox) c).Text = "";
      }
   }
And you can modify this to handle those inside Panels and other container controls.
If you have CheckBoxes or text fields inside custom controls, though, you will have to work out your own way of dealing with them.
 
Share this answer
 
Comments
nkhldhar 25-Jun-12 4:31am    
do i have to replace c with something
Sergey Alexandrovich Kryukov 25-Jun-12 20:43pm    
Ineffective code, if you are going to use c below the check, as should be used for dynamic case. Also, use string.Empty, not string literal "".
--SA
OriginalGriff 26-Jun-12 2:54am    
The string.Empty vs "" debate is down to personal taste, and reability. They generate the same IL code.
There is an example on SO : http://stackoverflow.com/questions/263191/in-c-should-i-use-string-empty-or-string-empty-or

The IS or AS debate is fun too: AS is faster if you are going to test and likely use the result, IS is faster if you are going to test and likely not use the result. So in Event handlers, I use "sender AS control" and in tests like the above where it is more likely that the test will fail I use IS. If you pass IS, there is no need to add the extra check-again overhead of AS where the cast is guaranteed to work. :laugh:
Sergey Alexandrovich Kryukov 29-Jun-12 19:49pm    
I know about the same functionality, but you won't say that style does not matter, will you? :-)
This is not just test, there is one practical benefit for using string.Empty. It is very practical to clear code from immediate constants, for the sake of better maintenance. At the same time, they helps to develop code faster on starting phases of development, so cleaning later makes sense. With strings, this is simple: search the whole file for '"'. If you make the only exclusion for "", it makes it difficult.

Can you see my point?

Thank you,
--SA
For a text box

this.textbox1.text = "";

For a check box

this.checkbox.checked = false;
 
Share this answer
 
On the reset button event handler

C#
protected void reset_Click(objest sender, EventArgs e)
{
  textBox1.Text = "";
  textBox2.Text = "";
  checkBox1.IsChecked = false;
  ....
}


Do the above code
 
Share this answer
 
v2
What the op probably wants is to iterate over the collection of controls and reset them, rather than referring to each individually.

Not if front of the computer right now, but pseudo code would be...

For each control in Form1.Controls

If control is a textbox control.text = ""
If control is a checkbox control.checked = false

Etc.
 
Share this answer
 
Hi,
In the button click event

C#
Textbox1.text = "";
Checkbox1.checked = false;


or

For text box you can use
C#
textbox1.clear();


Is this you want.
 
Share this answer
 
v2
C#
public void ClearTextBoxes(Control control)
        {
            foreach (Control c in control.Controls)
            {
             // Clear All Text Box
                if (c is TextBox)
                {
                    ((TextBox)c).Clear();
                }
             // Clear All Combo box
                if (c is ComboBox)
                {
                    ((ComboBox)c).Text = "";
                }
             // Clear All Check Box
                if (c is CheckBox)
                {
                    ((CheckBox)c).Checked = false;
                }
            }
        }
 
Share this answer
 
Comments
Sai Prasad anumolu 18-Feb-14 4:02am    
How to recheck The checkbox ...I am using checkbox popup
Just add a if clause in your check box event handler.

I will explain it to you via the following code

private void checkBox6_CheckedChanged(object sender, EventArgs e)
{
if(checkBoxname.checked)
{ // what you want to do when check box is checked
}
else

{//what you want to do when check box is unchecked, for instance if
// this,textBoxname.Text = ""; it will clear a text box
}
}
 
Share this answer
 

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