Share via


Visual Basic Concepts

Using the Command Button Control

The command button control is used to begin, interrupt, or end a process. When clicked, it invokes a command that has been written into its Click event procedure.

Figure 7.10   The command button control

Most Visual Basic applications have command buttons that allow the user to simply click them to perform actions. When the user chooses the button, it not only carries out the appropriate action, it also looks as if it's being pushed in and released and is therefore sometimes referred to as a push button.

For More Information   See "Clicking Buttons to Perform Actions" in "Forms, Controls, and Menus" for a simple demonstration of the Command Button control.

Adding a Command Button to a Form

You will likely use one or more command buttons in your application. To add command buttons to a form, draw them on as you would any other control. Command buttons can be sized with the mouse or by setting their Height and Width properties.

Setting the Caption

To change the text displayed on the command button, use the Caption property. At design time, you can set this property by selecting it from the control's Properties window. When you set the Caption property at design time, the button text will be updated dynamically.

You can set the Caption property up to 255 total characters. If your caption exceeds the width of the command button, it will wrap to the next line. However, it will be clipped if the control cannot accommodate its overall height.

You can change the font displayed on the command button by setting its Font property.

Creating Keyboard Shortcuts

You can use the Caption property to create access key shortcuts for your command buttons by adding an ampersand (&) before the letter you want to use as the access key. For example, to create an access key for the caption "Print" you add an ampersand before the letter "P": "&Print". At run time, the letter "P" will be underlined and the user can select the command button by simultaneously pressing ALT+P.

Note   To include an ampersand in a caption without creating an access key, include two ampersands (&&). A single ampersand is displayed in the caption and no characters are underlined.

Specifying the Default and Cancel Properties

On each form, you can select a command button to be the default command button — that is, whenever the user presses the ENTER key the command button is clicked regardless of which other control on the form has the focus. To specify a command button as default set the Default property to True.

You can also specify a default cancel button. When the Cancel property of a command button is set to True, it will be clicked whenever the user presses the ESC key, regardless of which other control on the form has the focus.

Selecting the Command Button

A command button can be selected at run time by using the mouse or keyboard in the following ways:

  • Use a mouse to click the button.

  • Move the focus to the button by pressing the TAB key, and then choose the button by pressing the SPACEBAR or ENTER.

  • Press an access key (ALT+ the underlined letter) for a command button.

  • If the command button is the defaultcommand button for the form, pressing ENTER chooses the button, even if you change the focus to a different control.

  • If the command button is the default Cancel button for the form, then pressing ESC chooses the button, even if you change the focus to another control.

The Value Property

Whenever the command button is selected, its Value property is set to True and the Click event is triggered. False (default) indicates the button isn't chosen. You can use the Value property in code to trigger the command button's Click event. For example:

cmdClose.Value = True

The Click Event

When clicked, the command button's Click event is triggered and the code you've written in the Click event procedure is invoked.

Clicking a command button control also generates the MouseDown and MouseUp events. If you intend to attach event procedures for these related events, be sure that their actions don't conflict. The order in which these three events occur varies from control to control. In the command button control, these events occur in this order: MouseDown, Click, MouseUp.

Note   If the user attempts to double-click the command button control, each click will be processed separately; that is, the command button control does not support the double-click event.

For More Information   See "Responding to Mouse and Keyboard Events" for more information on the MouseDown and MouseUp events.

Visually Enhancing the Command Button

The command button control, like the check box and option button controls, may be visually enhanced by altering the setting of the Style property and then using the Picture, DownPicture and DisabledPicture properties. For example, you may want to add an icon or bitmap to a command button or display a different image when the button is clicked or disabled.