Controls and Components


Most controls are graphic by nature. Buttons, text boxes, and labels provide graphical input and feedback for the user. They display data and let the user trigger program actions. Some controls (such as grid controls, tree view controls, and calendar controls) are quite powerful and provide a rich variety of tools for interacting with the user.

On the other hand, components are represented by graphical icons at design time, and they are hidden at runtime. They may display some other object (such as a dialog box, menu, or graphical indicator), but the component itself is hidden from the user.

Generally, your code could use a component’s features at runtime without requiring you to place an object on a form. For example, the program could easily call a method that displays a dialog box. However, putting important functions in components rather than making them subroutine calls provides several benefits.

First, because you can add a component to a form graphically at design time, you don’t need to write code to instantiate it and set its properties at runtime. That would not be particularly difficult, but it would just be a little less convenient.

Adding a component to a form at design time also lets you manipulate it at design time instead of in code. You can use the Properties window to view and modify a component’s properties at design time. You can also use the code editor to make event handlers for the component (event-handler creation is described in greater detail later in this chapter).

Tip 

There are still times when it’s useful to create controls at runtime. For example, if you don’t know how many text boxes you will need until runtime, you cannot create them all at design time. Fortunately, you get the best of both worlds: Visual Basic lets you create controls at either design time or runtime.

Some components also provide information needed by graphical controls. For example, a program can use connection, data adapter, and data set components to define data that should be selected from a database. Then a grid control could display the data to the user. Because the connection, data adapter, and data set objects are components, you can define all this at design time without writing code.

Figure 2-2 shows a form at design time that contains several components. The components appear in the Component Tray at the bottom of the form, not on the form’s graphical surface.

image from book
Figure 2-2: Some components provide data for graphical controls.

This example contains four components. Timer1 fires an event periodically so the program can take some action at specified time intervals. ErrorProvider1 displays an error icon and error messages for certain controls on the form such as TextBoxes. BackgroundWorker1 performs tasks asynchronously while the main program works independently. ImageList1 contains a series of images. Usually an ImageList is associated with a control such as a Button, ListView, or TreeView, and provides images for the control.

Aside from the lack of a graphical component on the form, working with components is about the same as working with controls. You use the Properties window to set components’ properties, the code editor to define event handlers, and code to call their methods. The rest of this chapter focuses on controls, but the same concepts apply just as well to components.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net