C# 清除当前窗体中TextBox控件中的内容
//当有多个窗体时,对顶层的窗口进行操作,例如:我们开发具有录入功能的界面的时候,为了防止提交后的二次(重复)录入,希望点击提交按钮并提示成功后,界面的所有文本框内容能够自动清空
.NET Framework 类库
Form.ActiveMdiChild 属性
获取当前活动的多文档界面 (MDI) 子窗口。
命名空间:System.Windows.Forms
程序集:System.Windows.Forms(在 system.windows.forms.dll 中)
语法:public Form ActiveMdiChild { get; }
当窗体中不包含GroupBox控件时示例:
public void ClearAllChildFormText() { // 获取当前激活的窗口 Form tempChild = this.ActiveMdiChild; if (tempChild != null) { //遍历所有控件 foreach (Control control in tempChild.Controls) { if (control is TextBox) { //清掉含有TexBox控件上的内容 control.Text = ""; } /* //以下方法同上也能实现 TextBox textbox= control as TextBox; if (textbox!= null) { //清掉含有TexBox控件上的内容 textbox.Text = ""; } */ } } }
当窗体中包含GroupBox控件时,需要再次遍历GroupBox中的控件,示例:
foreach (Control control in this.Controls) { if ((control as GroupBox) != null) { foreach (Control tempcontrol in control.Controls) { if (tempcontrol is TextBox) { //清掉含有TexBox控件上的内容 tempcontrol.Text = ""; } /* //以下方法同上也能实现 TextBox textbox= tempcontrol as TextBox; if (textbox!= null) { //清掉含有TexBox控件上的内容 textbox.Text = ""; } */ } } }