Command buttons in visual basic

This is an article on Command buttons in visual basic in Visual Basic [VB].

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.

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 he wants to start.

You can set some properties only at design time (such as a control's Name property). You can set some properties (such as a caption) both at design time and at runtime inside event procedures and other module code, and you can set some properties (such as a list box's entries) only at runtime from within the program. All the control's properties that appear in the Properties window can be set at design time, and some you can set at runtime as well. As you learn more about Visual Basic, you will become familiar with the properties you can set only at runtime.

You will 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. you will also set the Left, Height, Top, and Width properties when you size and locate the command button because," these properties update automatically when you place and size controls.

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.

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.

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

A command button's Cancel property relates somewhat to focus. Whereas the focus determines which control gets the Enter keypress, a command button's Cancel property determines which command button gets a simulated Click event when the user presses the Esc key.Often, a command button used to exit an application or close a dialog box has its Cancel property set to True. Therefore, you can close such applications or dialog boxes by clicking the command button or by pressing Esc.

A command button's Default property also relates somewhat to focus. When a form first appears, the command button with the Default property of True receives the Click event when the user presses Enter. Another control might have the focus at that time, but if a command button has a Default property value of True, that button receives a Click event when the user presses Enter—unless the user moves the focus to another command button before pressing Enter. Only one command button can have a Default value of True at any one time. As soon as you assign a command button's Default value to True, either at design time or at runtime, any other command button on the form with a True Default value immediately changes to False. Therefore, Visual Basic protects a form's integrity by ensuring that only one command button can have a True Default value at any one time.

An analysis that I was doing about the command button is the fact that is so important in Windows applications. Would there ever be programs if buttons didn't exist? I don't think so... I think that buttons are everywhere and in all programs, some are just graphical and some not but buttons are needed in all programs to let the user decide what action to take....

Luis Lazo

Last edited by shabbir; 1May2008 at 11:30..
Reason: Confine links to signature