Search code examples
c#resetgroupbox

Method to reset members inside groupBox


is there any method for groupBox to clear all properties of objects inside groupBox. for example clear all textboxes, deselect all checkboxes etc. and set them to default. or i should code one by one to clear them? i want to do this on event listview SelectedIndexChanged.

Update:

ok thanks for replays, i found that you can select controls inside groupbox very simple.

        foreach (Control ctrl in groupBox2.Controls)//this will only select controls of groupbox2
        {
            if (ctrl is TextBox)
            {
                (ctrl as TextBox).Text = "";
            }
            if (ctrl is CheckBox)
            {
                (ctrl as CheckBox).Checked = false;
            }
            if (ctrl is ComboBox)
            {
                (ctrl as ComboBox).SelectedIndex = -1;
            }
            //etc
        }

Solution

  • The fastest way to do that is :

    Control myForm = Page.FindControl("Form1");
    foreach (Control ctrl in myForm.Controls)
    {
        //Clears TextBox
        if (ctrl is System.Web.UI.WebControls.TextBox)
        {
            (ctrl as TextBox).Text = "";
        }
        //Clears DropDown Selection
        if (ctrl is System.Web.UI.WebControls.DropDownList)
        {
             (ctrl as DropDownList).ClearSelection();
        }
        //Clears ListBox Selection
        if (ctrl is System.Web.UI.WebControls.ListBox)
        {
            (ctrl as ListBox).ClearSelection();
        }
        //Clears CheckBox Selection
        if (ctrl is System.Web.UI.WebControls.CheckBox)
        {
            (ctrl as CheckBox).Checked = false;
        }
        //Clears RadioButton Selection
        if (ctrl is System.Web.UI.WebControls.RadioButtonList)
        {
            (ctrl as RadioButtonList).ClearSelection();
        }
        //Clears CheckBox Selection
        if (ctrl is System.Web.UI.WebControls.CheckBoxList)
        {
            (ctrl as CheckBoxList).ClearSelection();
        }
    }