原文: 遍历子控件清空数据

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CLeopard/article/details/41446353

遍历窗体上所有控件,并清空数据,重要的在于遍历所有控件,这里写了个例子。

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

<span style="white-space:pre">	</span>/// <summary>
        /// 遍历子控件清空-TestCLeopard
        /// </summary>
        /// <param name="CL"></param>
        private void ClearIt(Control CL)
        {
            try
            {
                foreach (Control C in CL.Controls)
                {
                    if (C.GetType().ToString().Contains("TextBox"))
                    {
                        ((TextBox)C).Text = "";
                    }
                    else if (C.GetType().ToString().Contains("ComboBox"))
                    {
                        ((ComboBox)C).SelectedIndex = -1;
                    }
                    else if (C.GetType().ToString().Contains("DateTimePicker"))
                    {
                        ((DateTimePicker)C).Text = DateTime.Now.ToString();
                    }
                    else if (C.Controls.Count > 0)
                    {
                        ClearIt(C);
                    }
                }
            }
            catch
            {
                return;
            }
        }
直接调用即可。

例如:

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确定要清空吗?", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (result == DialogResult.OK)
            {
                ClearIt(this);
            }
        }





扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄