Text Validation

This sample shows how you can create a set of special-purpose text boxes that are designed for a specific type of input. This sample includes text boxes for e-mail addresses, IP addresses, and phone numbers.

The MaskedTextBox control provides validation as the user types, so incorrect text never appears in the MaskedTextBox.

The RegExTextBox class, derived from TextBox in this sample, serves at the base class for several other controls. This class validates the contents of the TextBox control against a given regular expression. When the user clicks the Validate button on the main form, the code loops through the controls collection to find all the RegExTextBox controls, then casts them to that base type to call the IsValid property. If the input is invalid, the TextBox text color will be red.


Download

Top-Level Forms

Many document-based applications support multiple windows, one per open document, which are displayed in the taskbar. The ALT+TAB key combination can be used to switch between the documents, but without the overhead incurred by loading each document in a separate process. This sample implements this behavior.

The startup object for the application is the Sub Main method inside a class named FormsManager. The Main method creates a new form instance by calling the NewForm method of the FormsManager class and then adds the new form to a list object. Once the first form is created, the code calls Application..::.Run to start the main application thread so that closing the first form does not shut down the process. Each document form exposes a New menu item. Clicking this menu item calls NewForm method again, which opens a new document form just as the Sub Main did as the application loaded.

Each form also provides Close and Exit menu items. Choosing Close starts the closing process. Each form has a Closing event which allows the form's code to check whether the document contents have been modified and, if so, to ask the user whether to save or not.

If you try to close a changed document by closing the form or exiting the application, you will see a dialog box asking you to save the form's contents. If you click Yes, the code calls the form's Save method and closes the form. If you click No, the form simply closes. If you click Cancel, the form does not close and a custom event is raised telling the application to stop shutting down. This application does not actually perform any file I/O to save the file.


Download

StatusStrip Control

Several types of data are displayed:

  • Text Text is displayed in a panel.

  • Keyboard status The status of the CAPS LOCK and NUM LOCK keys are displayed as text in a panel.

  • Date The date is displayed in the current culture in a panel.

  • Progress A ProgressBar control is displayed in the StatusStrip control.


    Download