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 = "";
                            }     
                        */
                    }
                }
            }        

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM