Home > DeveloperSection > Forums > Clear Checkboxes and Textboxes in Splitcontainer - C#
Ankita Pandey
Ankita Pandey

Total Post:183

Points:1285
Posted on    December-23-2013 2:43 AM

 C# C# 
Ratings:


 1 Reply(s)
 2570  View(s)
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?



Pravesh Singh

Total Post:411

Points:2881
Posted on    December-23-2013 5:05 AM

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.

Don't want to miss updates? Please click the below button!

Follow MindStick