簡體   English   中英

清除所有文本框(任何快捷方式)

[英]Clear All textBoxes (Any Shortcut)

我有幾個用於輸入的文本框,然后還有幾個用於結果的文本框。 是否有任何快捷方式可以清除表單上的所有條目?

例如aTextBox.Clear();

如果所有文本框都是表單的直接子級,則可以使用 LINQ:

yourForm.Controls.OfType<TextBox>().ToList().ForEach(textBox => textBox.Clear());

評論 Frederic - 您需要使用 ActiveForm yourform.ActiveForm.Controls. ... yourform.ActiveForm.Controls. ... .

因為你yourform.Controls. ... yourform.Controls. ...拋出一個錯誤:

An object reference is required for the non-static field, method, or property 'System.Windows.Forms.Control.Controls.get

申請VS2012Express

您將需要循環所有表單控件並檢查類型是否為文本框,then.Text = string.empty

var cntrlCollections = GetAll(this,typeof(TextBox));

       foreach (Control ctrl in cntrlCollections)
        {

            if (ctrl is TextBox)
            {
                ctrl.Text = " ";
            }
        }      

public IEnumerable<Control> GetAll(Control control, Type type)
    {
        var controls = control.Controls.Cast<Control>();

        return controls.SelectMany(ctrl=>GetAll(ctrl,type)).Concat(controls).Where
               (c=>c.GetType() ==type);
    }

在點擊事件中調用此 function 以清除所有文本框。 希望這對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM