CodeBreaker
CodeBreaker

Reputation: 87

How to clear TextBoxes nested inside a TabControl, TabPage and 2 Panels?

The picture below represents a plug-in I am building.

Yellow = tabControl1; Orange = tabPage1; Blue = Main Panel (mainPanel); Green = 3 different panels inside the main panel (panel1, panel2 and panel3); White = TextBoxes (that I want to clear); DarkBlue = CheckBoxes corresponsing to the green panels

plugIn

I am trying to clear any text from the TextBoxes that contain text and reset CheckBoxes when checked, with a button click.

I researched online and tried to accomplish this by the following way but it doesn't seem to be the correct way to handle this problem.

foreach (Control a in tabControl1.Controls)
{
    foreach(Control b in tabPage1.Controls)
    {
        foreach (Control c in mainPanel.Controls)
        {
            foreach (Control d in panel1.Controls)
            {
                if (d is TextBox)
                {
                    d.Text = " ";
                }
                if (d is CheckBox)
                {
                    ((CheckBox)d).Checked = false; 
                }
            }
        }
    }
}

I have only shown panel1 here but tried doing the same thing for panel2 and 3 as well.

What exactly am I doing wrong here? Any help would be greatly appreciated!

Upvotes: 0

Views: 358

Answers (2)

Jimi
Jimi

Reputation: 32288

You just need a simple recursive method that iterates all controls inside all child containers of a specified parent container, mainPanel, here.
If a control is of type TextBox or CheckBox, set its Text or Checked property accordingly:

(Note that you can also pass tabPage1 to the method, or any other ancestor)

ClearControls(panel1);
// or
ClearControls(tabPage1);

private void ClearControls(Control parent)
{
    if ((parent == null) || (!parent.HasChildren))
        return;
    foreach (var ctl in parent.Controls.OfType<Control>())
    {
        if (ctl is TextBox txt) {
            txt.Clear();
        }
        if (ctl is CheckBox chk) {
            chk.Checked = false;
        }
        else {
            if (ctl.HasChildren) {
                ClearControls(ctl);
            }
        }
    }
}

Upvotes: 2

Joshua Robinson
Joshua Robinson

Reputation: 3549

If you want to clear the TextBoxes and ComboBoxes inside of panel1 then you only need to loop through panel1's controls to do it. To handle any panel, you could just write a ClearPanel(Panel) function.

private void ClearPanel(Panel panel)
{
   foreach (var ctrl in panel.Controls)
   {
      if (ctrl is TextBox tb)
      {
         tb.Clear();
      }
      else if (ctrl is CheckBox chkBx)
      {
         chkBx.Checked = false;
      }
   }
}

private void ClearPanel1Button_Click(object sender, EventArgs e)
{
   ClearPanel(panel1);
}

Iterating through the controls in tabControl1, tabPage1, etc, etc adds unnecessary overhead if you already have a reference to the Panel you're dealing with. What's more, if any of the controls in the outer loops have an empty Controls collection then the inner loops won't execute. I suspect this might be why your code isn't working.

Note that the above solution will not handle any TextBox or ComboBox that is inside another container inside panel1. So, if panel1 had a GroupBox inside it which contained TextBoxes or ComboBoxes, they wouldn't be cleared. See the recursive solution below to handle that situation.

EDIT: After re-reading your question I thought maybe you want to clear all TextBox and CheckBox controls on the Form.

If you're needing to clear every TextBox or CheckBox on the Form, you can do this with recursion.

private void Clear(Control ctrl)
{
    if (ctrl is TextBox tb)
    {
       tb.Clear();
    }
    if (ctrl is CheckBox chkBx)
    {
       chkBx.Checked = false;
    }
    foreach (Control child in ctrl.Controls)
    {
       Clear(child);
    }
}

private void ClearButton_Click(object sender, EventArgs e)
{
   Clear(this);
}

You could pass any Control to Clear(Control), so if you only wanted to do the TextBoxes and ComboBoxes on tabPage1 you could call Clear(tabPage1). This will clear all the TextBoxes and ComboBoxes on tabPage1, even if they're in a Panel or GroupBox or some other container.

Upvotes: 1

Related Questions