Using the CommandButton control
Command button controls are used to start, interrupt, or end a process. When it is clicked the command that has been written into the Click event procedure is called.
There are command buttons in most Visual Basic applications that users can click to perform actions. When clicked, the button not only performs the corresponding action, but also appears to be pressed and released, so it is sometimes called a push button.
For more information about a simple example of the CommandButton control, see "Click a Button to Perform Actions" in Chapter 3, "Forms, Controls, and Menus."
Add command buttons to a form
You will most likely use one or more command buttons in your application. Just like drawing buttons on other controls, add command buttons to the form. The size of the command button can be adjusted with the mouse or by setting the Height and Width properties.
Set title
You can use the Caption property to change the text displayed on the command button. At design time, you can set this property in the control's Properties window. The button text will be dynamically updated after setting the Caption property at design time.
The Caption attribute can contain up to 255 characters. If the title exceeds the width of the command button, it will be folded to the next line. However, if the control cannot accommodate its full length, the title will be clipped. You can change the font displayed on the command button by setting the Font property.
Create a keyboard shortcut
You can create a command button's access key shortcut through the Caption property. To do this, just add a hyphen (&) before the letter that is the access key. For example, to create an access key for the title PRint, add a hyphen before the letter P, thus getting &Print. When running, the letter P will be underlined, and the command button can be selected by pressing the ALT P key at the same time.
Note If you do not create an access key, and you want the title to contain a hyphen without creating an access key, add two hyphens (&&). This will display only a hyphen and no underline in the title.
Specify Default and Cancel properties
A command button can be selected as the default command button in the upper part of each form. That is to say, no matter which control on the form has the focus, as long as the user presses the ENTER key, the default button has been clicked. In order to specify a default command button, its Default property should be set to True.
You can also specify a default Cancel button. After setting the Cancel property of the command button to True, no matter which control on the form has the focus, if you press the ESC key, the default button will be clicked.
Selected command button
At runtime, you can use the mouse or keyboard to select a command button using the following methods:
Click the button with the mouse.
Press the TAB key to shift focus to the button, then press the SPACEBAR or ENTER key to select the button.
Press the command button's access key (the underlined letter ALT).
If the command button is the default command button for the form, you can press the ENTER key to select the button, even if the focus has been transferred to another control.
If the command button is the default cancel button of the form, you can press the ESC key to select the button, even if the focus has been transferred to another control.
Value attribute
Whenever a command button is selected, its Value property is set to True and the Click event is fired. False (default) indicates that the button is not selected. You can use the Value property in code to trigger the Click event of the command button. For example:
cmdClose.Value=True
Click event
When a command button is clicked, the button's Click event is fired and the code that has been written in the Click event procedure is called.
MouseDown and MouseUp events are also generated after clicking the command button. If you want to attach event procedures to these related events, you should ensure that the operations do not conflict. Depending on the control, the order in which these three event processes occur is also different. The order of events in the CommandButton control is:
MouseDown, Click, MouseUp.
Note that if the user attempts to double-click the command button control, each click will be handled separately; that is, the command button control does not support double-click events.
For more information about the MouseDown and MouseUp events, see Chapter 11, "Responding to Mouse and Keyboard Events."
Enhance the visual effect of command buttons
Command buttons, like check boxes and option buttons, can be enhanced with the Picture, DownPicture, and DisabledPicture properties by changing the Style property setting. There may be situations where you want to add an icon or bitmap to a command button, or display a different image when the control is clicked or disabled.
->