Reputation: 23
I have one form which contain the my user control. How to clear all then textbox of that user control using a button in my main form?## Heading ##
Upvotes: 0
Views: 749
Reputation: 5796
You can use this method to clear all TextBoxes, just call it from your main form:
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);
}
Upvotes: 1