//當有多個窗體時,對頂層的窗口進行操作,例如:我們開發具有錄入功能的界面的時候,為了防止提交后的二次(重復)錄入,希望點擊提交按鈕並提示成功后,界面的所有文本框內容能夠自動清空
.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 = "";
}
*/
}
}
}