CHOOSING CONTROLS

Keeping all of the intricacies of each of these controls in mind at once is a daunting task. With so many powerful tools to choose from, it’s not always easy to pick the one that’s best for a particular situation.

To simplify error-handling code, you should generally pick the most restrictive control that can accomplish a given task, because more restrictive controls give the user fewer options for entering invalid data.

For example, suppose that the user must pick from the choices Small, Medium, and Large. The application could let the user type a value in a TextBox control, but then the user could type Huge or Weasel. The program would need to verify that the user typed one of the valid choices and display an error message if the text was invalid. The program might also need to use precious screen real estate to list the choices to help the user remember what to type.

A better idea would be to use a group of three RadioButton controls or a ComboBox with DropDownStyle set to DropDownList. Then the user can easily see the choices available and can only select a valid choice. If the program initializes the controls with a default value rather than leaves them initially undefined, it knows that there is always a valid choice selected.

COMMON SENSE DEFENSE
Restrictive controls also make the application more secure. By presenting users with a list of choices rather than letting them type in whatever they like, the program can protect itself from attack. For example, ...

Get Visual Basic 2012 Programmer's Reference 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.