C# WinForm 中批量清除文本框

        /// <summary>
        /// 清空所有的文本框
     /// </summary>
        private void ClearAllTextBox()
        {
            foreach (Control control in this.groupBox1.Controls)
            {               
                if(control is TextBox)
                {
                    ((TextBox)control).Text = "";
                }
            }
        }

 其中IF语句也可以写成这样:

 if (control.GetType().ToString()=="System.Windows.Forms.TextBox")

 

如果按钮批量清空所有TextBox、comboBox、checkBox的数据,则代码可以写在这样:

       private void ClearTextBoxAndComboBoxAndCheckBox()
        {

            foreach (Control c in  this.Controls)
            {
                if (c.GetType().ToString().Contains("TextBox"))
                {
                    ((TextBox)c).Text = "";
                }
                if (c.GetType().ToString().Contains("ComboBox"))
                {
                    ((ComboBox)c).Text = "";
                }
                if (c.GetType().ToString().Contains("CheckBox"))
                {
                    ((CheckBox)c).Checked = false;
                }
            }
        } 

 

你可能感兴趣的:(C++,c,windows,C#,WinForm)