Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

Command Buttons

Command buttons appear in almost every window of every Windows application.

Command buttons determine when the user wants to do something such as exit the application or begin printing. In almost every case, you will perform these tasks to add a command button to an application:

  1. Locate and size the command button on the form.
  2. Change the command button's Name and Caption properties. (The Caption property holds the text that appears on the command button.)
  3. Add code to the command button's Click event procedure.

Although the command button control supports dozens of properties, you'll set only the Name and Caption properties in most cases. In addition, although command button controls support over a dozen events, you'll write code only for the Click event in most cases. After all, a command button resides on most forms so that the user can click the button to trigger some event that she wants to start.

Although you'll set the command button's Name and Caption properties most of the time, setting the Caption property often requires that you change the font to increase or decrease the text size and style on the caption. Of course, you might want to center the caption text or, perhaps, left-justify or right-justify the text, so you also might need to change the Alignment property. In reality, you'll also set the Left, Height, Top, and Width properties when you size and locate the command button because, as you learned in Hour 3, "Controls and Properties," these properties update automatically when you place and size controls.

As you can see, although you seem to set only a few properties for most controls, the other properties really do play important roles, and you'll almost always end up setting several properties to finalize your application. Table 4.1 lists some of the most common command button properties that you'll set.

An icon is a small graphical image that often appears on toolbar buttons. Icons are stored in a file with the .ico filename extension.

Table 4.1. Common command button properties.

Property Description
BackColor Specifies the command button's background color. Click the BackColor's palette down arrow to see a list of colors, and click System to see a list of common Windows control colors. Before the command button displays the background color, you must change the Style property from 0-Standard to 1-Graphical.
Cancel Determines whether the command button gets a Click event if the user presses Esc.
Caption Holds the text that appears on the command button.
Default Determines if the command button responds to an Enter keypress even if another control has the focus.
Enabled Determines whether the command button is active. Often, you'll change the Enabled property with code at runtime when a command button is no longer needed and you want to gray out the command button.
Font Produces a Font dialog box in which you can set the caption's font name, style, and size.
Height Holds the height of the command button in twips.
Left Holds the number of twips from the command button's left edge to the Form window's left edge.
MousePointer Determines the shape of the mouse cursor when the user moves the mouse over the command button.
Picture Holds the name of an icon graphic image that appears on the command button as long as the Style property is set to 1-Graphical.
Style Determines whether the command button appears as a standard Windows command button (if set to 0-Standard) or a command button with a color and possible picture (if set to 1-Graphical).
TabIndex Specifies the order of the command button in the focus order.
TabStop Determines whether the command button can receive the focus.
ToolTipText Holds the text that appears as a ToolTip at runtime.
Top Holds the number of twips from the command button's top edge to the Form window's top edge.
Visible Determines whether the command button appears or is hidden from the user. (Invisible controls cannot receive the focus until the running code changes the Visible property to True.)
Width Holds the width of the command button in twips.

Share ThisShare This

Informit Network