CLEAR CHECKBOXES AND TEXTBOXES IN SPLITCONTAINER - C#

Ankita Pandey

Total Post:183

Points:1285
Posted by  Ankita Pandey
C# 
C#
 3394  View(s)
Ratings:
Rate this:

I'm attempting to clear all the checkboxes and textboxes in the first panel of my splitcontainer but I'm having no luck as nothing seems to happen.

private void clearSettingsBtn_Click(object sender, EventArgs e)
{
    foreach (Control c in splitContainer1.Panel1.Controls)
    {
        if (c is CheckBox)
        {
            ((CheckBox)c).Checked = false;
        }
        else if (c is TextBox)
        {
            ((TextBox)c).Clear();
        }
    }
}

Have I missed something?

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: Clear Checkboxes and Textboxes in Splitcontainer - C#

    Hi Ankita,

    Actually your code is pretty OK, there should be another problem like wrong panel or more likely there would be another container in your panel, like a GroupBox, which in that case (another container) you should iterate through that container.

    In case you have a container inside the panel a simple code could be like this:

                foreach (Control control in splitContainer1.Panel2.Controls)
            {
                if (control is GroupBox)
                {
                    foreach (Control child in (control as GroupBox).Controls)
                    {
                        if (child is CheckBox)
                        {
                            ((CheckBox)control).Checked = false;
                        }
                        else if (child is TextBox)
                        {
                            (control as TextBox).Clear();
                        }
                    }
                }
            }
    But if you want to write a more general code, you should check for each container, i.e. GroupBox, Panel etc.

      Modified On Apr-04-2018 12:46:17 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!