O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required