John Advincula
John Advincula

Reputation: 23

c# how to clear textboxes of user control from main form

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

Answers (1)

mrid
mrid

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

Related Questions