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:
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:
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.