`
cpine
  • 浏览: 53180 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

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

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics