Invisible Controls: Using Components
There are several components that you can use in Windows Forms applications to help you perform a particular task. For example, you can use an ErrorProvider component to help validate data entered into a Windows Forms control. Components are available in the Toolbox, and you can drag them to a Windows Form just as you drag controls. However, the main difference between controls and components is that components typically do not have a user interface, and you cannot see them on the form.
When you drag a component to a form, it is added to the component tray, an area at the bottom of the Design view. Many of the available components are listed on the Components tab of the Toolbox. You will find additional components on other tabs, such as the Dialogs tab, the Data tab, and the Menus & Toolbars tab.
In this lesson, you will add some TextBox controls to a Windows Form. You will then use an ErrorProvider component to help validate data that is entered into one of the text boxes. You can write code in the Validating event handler of a control, such as a TextBox, to ensure that the data a user entered is valid. If the data is not valid, an error icon appears next to the control. Users can view the error by resting the mouse pointer on the error icon.
To validate data by using an ErrorProvider component
On the File menu, click New Project.
The New Project dialog box appears.
Click Windows Forms Application and then click OK.
A new Windows Forms project opens.
Add a Label control to the form, leaving the default name Label1, and then change the Text property of the control to Name.
Add a TextBox control next to the Label control, leaving the default name TextBox1.
Add another Label control to the form, leaving the default name Label2, and then change the Text property of the control to Age.
Add a TextBox control next to this Label control, leaving the default name TextBox2.
Drag an ErrorProvider component from the Components tab of the Toolbox to the form, leaving the default name ErrorProvider1.
The component is added to the component tray.
Open the Code Editor by right-clicking the form and clicking View Code.
In the Code Editor, in the Class Name drop-down list, click TextBox2.
In the Method Name drop-down list, click Validating to create the event handler.
Add the following code to the TextBox2_Validating event handler. This code checks whether the data entered in TextBox2 is a numeric value. If it is not, the code sets the error message for the ErrorProvider component.
Press F5 to run the program.
When the form appears, type your name in the first text box, type your favorite color in the second text box, and then press the TAB key.
An error icon appears next to TextBox2.
Rest your mouse pointer on the error icon to display the error text.
Type your age, using numbers, in the text box, and press the TAB key.
The error icon disappears.
In this lesson, you learned how to add a component that enables you to validate data to a program.
In the next lesson, you will learn how to use some of the built-in dialog boxes in your programs.
Next Lesson: Reusing Controls: Working with Built-in Dialog Boxes