2017-06-06 17 views
0

我有一个包含我的用户控件的窗体。 ?如何在我的主要形式,用一个按钮来清除所有然后该用户控制的文本框##打头##c#如何从主窗体清除用户控件的文本框

+2

Winforms,ASP.NET,WPF,...?你可以在你的用户控件中提供一个公共方法'ClearTextBoxes',你可以从mainform调用它。然后逻辑封装在那里,f.e.像:'this.Controls.OfType ().ToList()的ForEach(T => t.Clear())' –

+0

感谢的想法.. 的foreach(控制在panel3.Controls X) { 如果(x is supplierUI) {((supplierUI)x).Clear(); } } 此代码适用于我。 –

回答

1

您可以使用此方法来清除所有文本框,只是把它从你的主要形式有:

public void ClearTextBoxes(bool searchRecursively = true) 
{ 
    Action<Control.ControlCollection, bool> clearTextBoxes = null; 
    clearTextBoxes = (controls, searchChildren) => 
    { 
     foreach (Control c in controls) 
     { 
      TextBox txt = c as TextBox; 
      txt?.Clear(); 
      if (searchChildren && c.HasChildren) 
       clearTextBoxes(c.Controls, true); 
     } 
    }; 

    clearTextBoxes(this.Controls, searchRecursively); 
}