WinForm 清空界面控件值的小技巧

一般来说清空一个界面的方法都是这样的

 private void btnClear_Click(object sender, EventArgs e)
        {
            this.txbAccount.Text = "";
            this.txbAddress.Text = "";
            this.txbCmp.Text = "";
            this.chkCash.Checked = false;
            this.cmbDepart.Text = "";

            //.............窗体里面各类要清空控件;
        }

但是这样太耗费时间跟精力了,所以这样来

private void btnClear_Click(object sender, EventArgs e)
{
     ClearCntrValue(this.pnlContent);
}
        /// <summary>
        /// 清除容器里面某些控件的值
        /// </summary>
        /// <param name="parContainer">容器类控件</param>
        public void ClearCntrValue(Control parContainer)
        {
            for (int index = 0; index < parContainer.Controls.Count; index++)
            {
                // 如果是容器类控件,递归调用自己
                if (parContainer.Controls[index].HasChildren)
                {
                    ClearCntrValue(parContainer.Controls[index]);
                }
                else
                {
                    switch(parContainer.Controls[index].GetType().Name)
                    {
                        case "TextBox":
                            parContainer.Controls[index].Text = "";
                            break;
                        case "RadioButton":
                            ((RadioButton)(parContainer.Controls[index])).Checked = false;
                            break;
                        case "CheckBox":
                            ((CheckBox)(parContainer.Controls[index])).Checked = false;
                            break;
                        case "ComboBox":
                            ((ComboBox)(parContainer.Controls[index])).Text = "";
                            break;
                    }
                }
            }
        }

原文作者:潇湘隐者

原文地址:http://www.cnblogs.com/kerrycode/archive/2010/02/05/1664267.html#

WinForm 清空界面控件值的小技巧

上一篇:[WPF系列]-数据邦定之DataTemplate 对分层数据的支持


下一篇:XDOC云服务API(四)