Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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

  1. On the File menu, click New Project.

    The New Project dialog box appears.

  2. Click Windows Forms Application and then click OK.

    A new Windows Forms project opens.

  3. Add a Label control to the form, leaving the default name Label1, and then change the Text property of the control to Name.

  4. Add a TextBox control next to the Label control, leaving the default name TextBox1.

  5. Add another Label control to the form, leaving the default name Label2, and then change the Text property of the control to Age.

  6. Add a TextBox control next to this Label control, leaving the default name TextBox2.

  7. 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.

  8. Open the Code Editor by right-clicking the form and clicking View Code.

  9. In the Code Editor, in the Class Name drop-down list, click TextBox2.

  10. In the Method Name drop-down list, click Validating to create the event handler.

  11. 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.

    
    If Not IsNumeric(TextBox2.Text) Then
        ErrorProvider1.SetError(TextBox2, 
            "You must enter a numeric value.")
    Else
        ErrorProvider1.SetError(TextBox2, "")
    End If
    
    
    
  12. Press F5 to run the program.

  13. 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.

  14. Rest your mouse pointer on the error icon to display the error text.

  15. 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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.