Clear All Controls on a Form

Problem

You want to clear all the input controls on a form.

Solution

Iterate recursively through the collection of controls, and clear the Text property whenever you find an input control.

Discussion

You can iterate through the controls on a form using the Form.Controls collection, which includes all the controls that are placed directly on the form surface. However, if any of these controls are container controls (such as a group box, a panel, or a tab page), they might contain more controls. Thus, it’s necessary to use recursive logic that searches the Controls collection of every control on the form.

The following example shows a form that calls a ClearControls function recursively to clear all text boxes. Figure 11-4 ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.