-2
I have a form with several Textbox, Combobox, Radiobutton. And I would like to clear all fields at once. I’ve seen a lot of forums, but I haven’t found what could be my mistake. When I compile it does not return any error and runs normally and when I click to clear the fields I filled does not happen anything. I created the method in the class and in the Button Click event I called it.
Na I tried to put the method in the same place where the event is Click and instantiating so: ClearForm(this.Control);
But nothing happens either.
Code in class:
public class Interface
{
public void ClearForm(System.Windows.Forms.Control parent)
{
foreach (System.Windows.Forms.Control ctrControl in parent.Controls)
{
if (Object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.TextBox)))
{
((System.Windows.Forms.TextBox)ctrControl).Text = string.Empty;
}
else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.ComboBox)))
{
((System.Windows.Forms.ComboBox)ctrControl).SelectedIndex = -1;
}
else if (object.ReferenceEquals(ctrControl.GetType(), typeof(System.Windows.Forms.RadioButton)))
{
((System.Windows.Forms.RadioButton)ctrControl).Checked = false;
}
}
}
}
}
In the Button Click event:
private void btnClear_Click(object sender, EventArgs e)
{
Interface inter = new Interface();
inter.ClearForm(this);
}
I left only the codes for Textbox, Combobox and Radiobutton. And no error appeared, but when I click to delete it appears a message (I put Try Catch) Sorry error occurred: Input string was not in a correct format. And I click OK and it erases some items, but the message remains and every time I click OK, it deletes some items. Can you tell me why this is happening?
– Cristina
hello Cristina, just seeing the same exception, and on which line is happening
– Rovann Linhalis
Hi!! I managed to find my mistake, thank you so much for the attention and attention!
– Cristina