重置groupBox内成员的方法 [英] Method to reset members inside groupBox

查看:63
本文介绍了重置groupBox内成员的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

groupBox是否有任何方法可以清除groupBox中对象的所有属性.例如,清除所有文本框,取消选择所有复选框等,然后将它们设置为默认值.还是我应该一一编码以清除它们?我想在事件列表视图SelectedIndexChanged上执行此操作.

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.

更新:

好的,感谢您的重播,我发现您可以非常简单地在groupbox中选择控件.

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
        }

推荐答案

最快的方法是:

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

这篇关于重置groupBox内成员的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆