9.8. Restrict a Text Box to Accepting Only Specific Input

Problem

You need to create a text box that will accept only the specified characters or keystrokes.

Solution

Use the MaskedTextBox control, and set the Mask property to configure the input that is acceptable.

How It Works

One way to ensure user input is valid is to prevent invalid data from being entered in the first place. The MaskedTextBox control facilitates this approach. The MaskedTextBox.Mask property takes a string that specifies the input mask for the control. This mask determines what type of input a user can enter at each point in the control's text area. If the user enters an incorrect character, the control will beep if the BeepOnError property is True, and the MaskInputRejected ...

Get Visual Basic 2008 Recipes: A Problem-Solution Approach 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.