Visual Basic Tutorials – Controls – The Command Button

Visual Basic Controls – The Command Button

Command buttons are the plain buttons that you simply click and release and are the most common type of buttons. These are the buttons you see everywhere in Visual Basic applications. They are usually just rounded, rectangular, gray buttons with a caption.

Command Button in Visual Basic

Command Button in Visual Basic

The easiest way to allow the user to interact with an application is to provide a command button to click. Visual Basic allows us to create two types of buttons: Command and Picture buttons.

Command buttons are also called push buttons and allow the user to click them to perform the required action. Image buttons display a picture on the screen. These visual basic controls are the most used controls in visual basic programming.

Here are some command buttons that you’d typically find in a program:

OK              Accepts a list of options and indicates that the user is ready to proceed.

Cancel      Discards a list of options.

Quit           Exits an open dialog box or program.

In each case, you should use command buttons carefully so that they work as expected when they are clicked.

Command Button Properties:

Appearance Selects 3-D or flat appearance.
Cancel Allows selection of button with Esc key (only one button on a form can have this property True).
Caption String to be displayed on button.
Default Allows selection of button with Enter key (only one button on a form can have this property True).
Font Sets font type, style, size.
Enabled Sets a value that determines whether a control can respond to user – generated events


Command Button Events:

Click      Event triggered when button is selected either by clicking on it or by pressing the access key.

Command Button Methods:

One of the methods used often with the command button is:

  • Move

Changing Command Button Properties

You can change command button properties (like those of all objects) in two ways:

  • By adjusting property settings in the Properties window.
  • By changing properties with program code.


Creating Button Control Arrays

You’ve decided that your new game program really does need 144 buttons in the main form, arranged in a grid of 12×12. But what a pain it is to write 144 sub-routines to handle the click event for each of them! Isn’t there a better way? There is. You use a control array and one event handler function (the control array index of the button that was clicked is passed to the event handler, so you can tell which button you need to respond to). To create a control array, just give two controls of the same type the same name (in the Name property); when you do, Visual Basic will ask if you want to create a control array.

When you create an event handler subroutine for a button in the control array, Visual Basic will automatically pass the index of the control in the control array to that subroutine:

Private Sub GamePiece_Click(Index As Integer)

End Sub

You can then refer to the control that caused the event as a member of an array, using the index passed to the subroutine:

Private Sub GamePiece_Click(Index As Integer)

GamePiece(Index).Caption = "You clicked me!"

End Sub


TIP: When you add controls to a control array, the first one has Index 0, the next has Index 1, and so on. You can change the index of each control with its Index property, rearranging the controls in the control array as you like.