重置groupBox内成员的方法

Method to reset members inside groupBox

groupBox有没有什么方法可以清除groupBox中对象的所有属性。例如清除所有文本框,取消select所有复选框等并将它们设置为默认值。或者我应该一个一个地编码来清除它们?我想在事件列表视图 SelectedIndexChanged 上执行此操作。

更新:

好的,感谢您的重播,我发现您可以select 非常简单地在组框内进行控制。

        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
        }

您需要将组框内的所有控件一一清除。

最快的方法是:

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();
    }
}

你必须创建这样的函数:

private void ClearControls(Control control)
{
    var textbox = control as TextBox;
    if (textbox != null)
        textbox.Text = string.Empty;

    var dropDownList = control as DropDownList;
    if (dropDownList != null)
        dropDownList.SelectedIndex = 0;

    // And add any other controls
    // ...

    foreach( Control childControl in control.Controls )
    {
        ClearControl( childControl );
    }
}

就这样称呼它:

ClearControls(this);

这将递归工作,因此如果您有任何面板,例如,有自己的一组控件要清除,这也会清除它们。