Click here to Skip to main content
15,886,023 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my form having multiple text/combo/richtext box. i want to erase all data from combobox,textbox and richtextbox in one button click.

i am using below code but it's not working with multiple controls.
C#
private void clear()
      {
          foreach (Control cntl in this.Controls)
          {
                if (cntl is TextBox && cntl is ComboBox && cntl is RichTextBox)
              {

                  ((TextBox)cntl).Text = string.Empty;
                  ((ComboBox)cntl).Text = string.Empty;
                  ((RichTextBox)cntl).Text = string.Empty;
              }
          }
      }
private void button1_Click(object sender, EventArgs e)
      {
          clear();
      }
Posted
Updated 19-Mar-14 7:36am
v3
Comments
$*Developer - Vaibhav*$ 19-Mar-14 10:26am    
write your if (cntl is TextBox && cntl is ComboBox && cntl is RichTextBox) in different if condition if (cntl is TextBox) {
TextBox txt = (TextBox)cntl;
txt.Text = "";}

if (cntl is ComboBox ) {
ComboBox cmb = (ComboBox )cntl;
cmb.Items.Clear();
}

Your condition is wrong.
C#
if (cntl is TextBox && cntl is ComboBox && cntl is RichTextBox)

How come one Control would be a TextBox, ComboBox and RichTextBox at the same time? This condition would never be satisfied.

Instead, just write conditions for individual cases. You can write Switch Case or multiple ifs.
 
Share this answer
 
v2
Comments
manish7664 19-Mar-14 15:24pm    
thanks for reply . but i want to ask one thing why we can give one control in textbox,combobox and richtext.
what the differance in both condition
if (cntl is TextBox && cntl is ComboBox && cntl is RichTextBox) and
if (cntl is TextBox)
{
//code
}
if(cntl is combobox)
{
//code

if (cntl is richtextbox )
{//code
}
Okay, let's analyze.

The if condition has three conditions. Suppose loop continue and now the control is a TextBox.
So, first condition is true, but second and third conditions are false. So, it will not go inside the if condition.

As you are already checking for individual control types, so no need to have one if condition like this before them.
First of all you are using && (AND) operator instead of || (OR) in your IF statement.
Second, you are casting one control to three types of Control in one loop pass.

Assuming that you want to clear only Text property of controls you have few solutions:

1. You need to check your control for every possible type you want to clear like this:
C#
foreach (Control cntl in this.Controls)
{
    if (cntl is TextBox)
    {
        ((TextBox)cntl).Text = string.Empty;
    }
    else if (cntl is ComboBox)
    {
        ((ComboBox)cntl).Text = string.Empty;
    }
    else if (cntl is RichTextBox)
    {
        ((RichTextBox)cntl).Text = string.Empty;
    }
}


2. Or, you can set Text property directly, beceause Control type has it, so any type that inherits from the Control will has it too. This will do the job:
C#
foreach (Control ctrl in this.Controls)
{
    ctrl.Text = string.Empty;
}


My advice is to put controls that you will be clearing on any container control, like Panel or GroupBox.

Hope it helps.
 
Share this answer
 
Comments
manish7664 19-Mar-14 14:52pm    
Thanks Mr Marcin .your code working fine. but i want to ask one thing you also using same controls (cntl) in multiple if condition.
manish7664 19-Mar-14 15:50pm    
it is possible to cast one control in three time .i've simple used this and it's working fine
foreach (Control cntl in this.Controls)
{
if (cntl is TextBox || cntl is ComboBox || cntl is RichTextBox)
{
cntl.Text = string.Empty;
cntl.Text = string.Empty;
cntl.Text = string.Empty;

}
}
Marcin Kozub 19-Mar-14 15:56pm    
You don't understand what you're doing here. You just do same thing three time, nothing more. I think that you must learn how foreach loop (or any loop) is working and what collections are.
Try to read those articles first:
Info about collections: http://msdn.microsoft.com/pl-pl/library/ybcx56wz.aspx
Info about loops: http://msdn.microsoft.com/en-us/library/f0e10e56%28v=vs.90%29.aspx
Info about Controls: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controls%28v=vs.110%29.aspx
If may sound a bit off-topic, but if I had to deal with a project where I need to clean up different controls in one loop I would add an extension method to all of the involved type (using the same name). That extension method will clean the control.
Now in my loop I would invoke that extension method using reflection...
 
Share this answer
 
Comments
Marcin Kozub 19-Mar-14 15:49pm    
You don't need reflection to use Extension methods.
public static class ControlExtensions
{
public static void ClearAllControlsText(this Control control)
{
foreach (Control ctrl in control.Controls)
{
ctrl.Text = string.Empty;
}
}
}

then inside Form: this.ClearAllControlsText();
Kornfeld Eliyahu Peter 20-Mar-14 2:29am    
This not my intention - I'm talking about an extension method on the control that know how to clear the specific control (remember, it's not always text = empty!). To call such extension methods you need reflection. You proposal adds nothing over a simple loop inside the click event handler...
Marcin Kozub 20-Mar-14 3:05am    
I understand your idea now.
if (cntl is TextBox) {
TextBox txt = (TextBox)cntl;
txt.Text = "";}

if (cntl is ComboBox ) {
ComboBox cmb = (ComboBox )cntl;
cmb.Items.Clear();
}
 
Share this answer
 
Comments
manish7664 19-Mar-14 14:27pm    
you also doing same thing . the diff. is you are giving same condition in multiple if block.
C#

private void clear()
{
foreach (Control cntl in this.Controls)
{
if (cntl is TextBox || cntl is ComboBox || cntl is RichTextBox)
{

((TextBox)cntl).Text = string.Empty;
((ComboBox)cntl).Text = string.Empty;
((RichTextBox)cntl).Text = string.Empty;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
clear();
}
try this
 
Share this answer
 
Comments
Marcin Kozub 19-Mar-14 15:44pm    
You didn't solve anything... You're casting on control in every loop pass three times...
Don't use any condition, simply using type casting, you can reset all the controls.
See my cod below,
C#
foreach (Control cntl in this.Controls)
{
    ((TextBox)cntl).Text = string.Empty;
    ((ComboBox)cntl).SelectedIndex= 0;
    ((RichTextBox)cntl).Text = string.Empty;
}


-KR
 
Share this answer
 
Comments
manish7664 19-Mar-14 14:53pm    
not working...
Krunal Rohit 19-Mar-14 15:10pm    
What's the error ??

-KR
manish7664 19-Mar-14 15:17pm    
Unable to cast object of type 'System.Windows.Forms.TextBox' to type 'System.Windows.Forms.ComboBox
Marcin Kozub 19-Mar-14 15:44pm    
It's totally wrong answer... How you can cast one control three times?

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