Answer the question
In order to leave comments, you need to log in
C# WinForm how to clear all TextBoxes on a form?
Hello, tell me how to clear all TextBoxes on the form if some of them are on GroupBoxes
Tried this
foreach(Control c in Controls)
if(c is TextBox)
((TextBox)c).Text = null;
And thisforeach (Control c in this.Controls)
{
if (c.GetType() == typeof(TextBox))
c.Text = string.Empty;
}
But it only cleans those text fields that are just on the form, and not on GroupBoxes
Answer the question
In order to leave comments, you need to log in
You have to go inside. Here is the simplest way to modify your code:
foreach (Control c in Controls)
{
if (c.GetType() == typeof (GroupBox))
foreach (Control d in c.Controls)
if (d.GetType() == typeof(TextBox))
d.Text = string.Empty;
if (c.GetType() == typeof(TextBox))
c.Text = string.Empty;
}
private static void CleanAllTextBoxesIn(Control parent)
{
foreach (Control c in parent.Controls)
{
if (c.GetType() == typeof(TextBox))
c.Text = string.Empty;
if (c.GetType() == typeof (GroupBox))
CleanAllTextBoxesIn(c);
}
}
private void CleanAllTextBoxesButton_Click(object sender, EventArgs e)
{
CleanAllTextBoxesIn(this);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question