Mastering Visual Basic .NET Tutorial 2: The Validation Project-Using the Error Provider Control

SYBEX Tutorial Mastering â„¢ Visual Basic ®.NET Evangelos Petroutsos Tutorial 2: The Validation Project—Using the ErrorProvider Control Copyright © 2002 SYBEX Inc., 1151 Marina Village … Tutorial 2 The Validation Project—Using the ErrorProvider Control The most mundane task in programming is (and has always been) the validation of the data. Every piece of data supplied by the user on a form must be validated before it’s processed. Even a simple value entered into an InputBox must be validated before it can be used in any …
When you start the application, you can use the File ? Load command to load a few contacts, or create a new list by clicking the Add button. The Add button is not shown in Figure 1, because this figure shows the form in the midst of an edit operation. While editing a contact (or adding a new one), the OK and Cancel buttons are displayed at the bottom of the form. The user must end the operation by clicking one of the two buttons. The ErrorProvider control is invisible at runtime and its icon will appear in the Controls tray below the form’s designer. A single instance of the control can display the error icon next to any number of fields, so you only need a single instance of the control on your form. The control’s basic properties are: Icon You can set this property to your own graphics file, if you want to display a custom image instead of the default icon. DataSource If you set the control’s DataSource property to a DataSet, ErrorProvider control will display an error icon for each row that contains an error. To display the error icon (default or custom) next to a control, call the SetError method, which accepts as arguments a reference to a control and a string. The string is the error message that will appear when the pointer hovers over the error icon. The following method will display an error icon with the specified error next to the TextBox1 control: ErrorProvider1.SetError(TextBox1, “This field can’t be blank”)… Other than the validation routines, the Validation project is a typical data entry form. It’s not connected to a database, but all contacts are stored into an ArrayList structure and the entire ArrayList is serialized to a disk file with the Serialization class. The complete source code appears in Listing 2.1. The application is discussed in detail in the section “The Serialization Class” of Chapter 11 in Mastering Visual Basic .NET . Listing 2.1: The Complete Validation Project Option Strict On Imports System.IO Imports System.runtime.Serialization Imports System.runtime.Serialization.Formatters.Binary Public Class ValidationForm Inherits System.Windows.Forms.Form #Region ” WindowsForm Designer generated code ” Public Sub New() MyBase.New() ‘This call is required by the WindowsForm Designer. InitializeComponent() ‘Add any initialization after the InitializeComponent() call End Sub ‘Form overrides Dispose Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing)…
Download Mastering Visual Basic .NET Tutorial 2: The Validation Project—Using the ErrorProvider Control.pdf