C#在WinForm中实现清空指定类型控件的内容

实现在Winform中递归控件来清空指定类型控件的内容(因为在Winform中,各个控件是有层次关系的,不能简单地依靠遍历this.controls)

private void ClearContent4Controls(Control.ControlCollection controls)
{
            foreach (Control c in controls)
            {
                if (c is TextBox)
                {
                    (c as TextBox).Text = string.Empty;
                }
                else if (c is ComboBox)
                {
                    (c as ComboBox).SelectedIndex = -1;
                }
                else if (c is CheckBox)
                {
                    (c as CheckBox).Checked = false;
                }

                ClearContent4Controls(c.Controls);
            }
}

posted @ 2009-06-10 17:06  迪卡.凯恩  阅读(1942)  评论(1编辑  收藏  举报