martin2k

Home | Forums | VB6 | Games | Software | Directory | HTML | Wiki | Blog | Contact

The CommandButton Control


 

Home

Read and add posts to the Visual Basic 6.0 Forums

Download some of my programs and OCXs

Read the Tips here

The old style forum is still available here

Contact


 

Beginner Tip

The CommandButton is used to make an action happen e.g. process the inputs that have been entered onto a Form and then show the output.

 

The CommandButton on a Form.
The CommandButton on a Form.
The CommandButton's icon on the Toolbox
The CommandButton's icon on the Toolbox

The naming convention for the CommandButton is 'cmd'.  E.g. cmdExit.  Also, CommandButton's look more professional if you set the Height property to 375, unless your creating Graphical buttons of course.

Properties

BackColor

Sets the colour of the CommandButton's background.  This property only takes effect when the Style property is set to 1 - Graphical.

Cancel

If this property is set to True, pressing the Escape key at runtime will activate the CommandButton's Click event.  Only one CommandButton on the Form can have this property set to True.

Caption

Sets the text to be displayed on the CommandButton.

If possible keep this as short as possible, so you don't have to resize the CommandButton.  You can always use the ToolTipText property if you want to give the user further information.

You can use mnemonics e.g. putting the '&' symbol somewhere in the Caption will cause the following character to be underlined at runtime.  When the user presses the Alt+[Underlined Letter] key combination, the CommandButton's Click event will be called.

Default

Works in a similar way to the Cancel property, but with the Enter key.  The CommandButton, which has this property set to True, will also appear with a darker border.

DisabledPicture

Sets the picture to be displayed when the Enabled property is set to False.  The Style property must be set to 1 - Graphical.

Using the DisabledPicture property.
Using the DisabledPicture property.

If the CommandButton was big enough, the caption would appear under the picture instead of over it.

DownPicture

Sets the picture to be displayed while the button is pressed down i.e. when the user holds the mouse button or the space key on it.  The Style property must be set to 1 - Graphical.

Enabled

When this property is set to True, the user is not able to interact with the CommandButton in any way e.g. it cannot be clicked on or have focus.  The caption will appear greyed out.

Font

In design time, this brings up the Font dialog box to allow you to choose the font name, style, size and whether the Strikeout and Underline attributes are set.  When writing code to set the font of the CommandButton, Font acts as an object, which has its own properties:

  • Bold
  • Charset
  • Italic
  • Name
  • Size
  • Strikethrough
  • Underline
  • Weight

For example:

Command1.Font.Bold = True
Command1.Font.Italic = True
Command1.Font.Underline = True

Alternatively, you can use the FontBold (Boolean), FontItalic (Boolean), FontName (String), FontSize (Single), FontStrikethru (Boolean) and FontUnderline (Boolean) properties, which are only available at runtime.

Height

Sets the height of the CommandButton.

Index

Leave blank unless the CommandButton is to be part of a control array.

Left

Sets the X coordinate of the CommandButton.

MaskColor

This property only comes into effect when the following conditions are met - the Style property is set to 1 - Graphical, the UseMaskColor property is set to True and a background picture is being displayed on the CommandButton.  The colour that is set in the MaskColor property will become the transparent colour in the picture.  E.g. if white is used in the picture, setting the MaskColor to white will mean that white changes the background colour of the CommandButton (i.e. changes to being transparent).

The MaskColor has been set to white, which means that the white areas in the picture have become transparent.
The MaskColor has been set to white, which means that the white areas in the picture have become transparent.

You should not use a system colour for the MaskColor property because system colours are changeable by the user.  This means that the CommandButton would end up with no colour being made transparent, which would not be the desired effect.

MouseIcon

When the user hover's the mouse pointer over the CommandButton, the image set for this property will become the mouse icon.  This property will only work if the MousePointer property is set to 99 - Custom.

MousePointer

Sets the type of mouse pointer to be used when the user hovers the mouse over the CommandButton.  The possible choices are:

Value Name Constant
0 Default vbDefault
1 Arrow vbArrow
2 Cross vbCrosshair
3 I-Beam vbIbeam
4 Icon vbIconPointer
5 Size vbSizePointer
6 Size NE SW vbSizeNESW
7 Size N S vbSizeNS
8 Size NW SE vbSizeNWSE
9 Size W E vbSizeWE
10 Up Arrow vbUpArrow
11 Hourglass vbHourglass
12 No Drop vbNoDrop
13 Arrow and hourglass vbArrowHourglass
14 Arrow and Question vbArrowQuestion
15 Size All vbSizeAll
99 Custom vbCustom

Picture

Sets the picture, which is displayed in the background of the CommandButton.  If the picture is smaller than the CommandButton, it will appear in the centre and the CommandButton's caption will appear underneath.  The Style property needs to be set to 1 - Graphical for this property to work.

Style

This needs to be set to 1 - Graphical if you want to use the graphical properties of the CommandButton (apart from Fonts).

TabIndex

Sets the order in which the controls will get focus when the tab key is pressed.  The control that has 0 as its TabIndex will have focus when the Form is loaded.

TabStop

If you set this to False, the CommandButton will not get focus when the tab key is pressed.

Tag

This property can be set to a string if you need to store any extra information related to the CommandButton.

ToolTipText

When this property is set, the text will popup over the CommandButton when the user hovers the mouse pointer over it.  This can be useful if you cannot fit all the text you need to in the Caption.

Top

Sets the Y coordinate of the CommandButton.

UseMaskColor

If set to True, the MaskColor property will be used as the transparent colour in the picture used on the CommandButton.  See the MaskColor property for information.

Visible

Determines whether the CommandButton appears on the Form.  I.e. if set to False, the CommandButton will be unavailable to the user - even by trying to set focus to it with the tab key.

Width

Sets the width of the CommandButton.

Methods

Move

Left As Single, [Top], [Width], [Height]

This method changes the position and optionally the size of the CommandButton.  The following example will move the CommandButton to the top-left of the Form and makes it into a square:

Command1.Move 0, 0, 375, 375

SetFocus

Sets the focus to the CommandButton.  This means that pressing the Enter key will activate the Click event, even if another CommandButton has it's Default property set to True.

Events

Click

This is probably the most important event for the CommandButton.  It occurs when the user clicks on the CommandButton, or activates it in another way e.g. with the Enter key, Space key, Alt+[Underlined Letter] or Escape key depending on the properties of the CommandButton.

Private Sub Command1_Click()
Text3.Text = CStr(Val(Text1.Text) + Val(Text2.Text))
End Sub

GotFocus

Occurs when the CommandButton gets focus either from the Tab key, when it is clicked on by the user or by using the SetFocus method.

KeyDown, KeyUp

KeyCode As Integer, Shift As Integer

When the user presses a keyboard key down or releases a key, these events occur.

Private Sub Command1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyBack Then
    MsgBox "You pressed the backspace key!"
End If
End Sub

The Shift argument contains the value of the Shift, Control and Alt keys.  This is useful for performing a different action when one of these keys is pressed in combination with another e.g. pressing 'A' would be different to pressing 'Ctrl+A'.

Constant Value Description
vbShiftMask 1 Shift key
vbCtrlMask 2 Ctrl key
vbAltMask 4 Alt key

If a combination of these keys are pressed, the Shift argument will contain the sum of all keys pressed e.g. Ctrl+Alt would be 6.

Private Sub Command1_KeyDown(KeyCode As Integer, Shift As Integer)
If Shift = vbCtrlMask + vbShiftMask Then
    If KeyCode = vbKeyA Then
        MsgBox "You pressed the Ctrl+Shift+A key combination!"
    End If
End If
End Sub

KeyPress

KeyAscii As Integer

This event works differently to the KeyDown and KeyUp events in that it detects the actual character (as the ANSI keycode) that is pressed rather than the keyboard key.  E.g.:

Key Pressed Result
s 115
Shift Nothing
Space 32

You can use the Chr function to convert the code into a letter e.g. Chr(KeyAscii).  If KeyAscii is set to 0 in this event, this makes the program act as if the key was never pressed.

LostFocus

Occurs when the CommandButton loses focus.

MouseDown, MouseUp

Button As Integer, Shift As Integer, X As Single, Y As Single

This event occurs when a mouse button is pressed down or released over a CommandButton.

Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbMiddleButton Then
    MsgBox "The middle button was pressed on the CommandButton"
End If
End Sub

The Button argument returns one of the following possible values:

Constant Value Description
vbLeftButton 1 Left mouse button
vbRightButton 2 Right mouse button
vbMiddleButton 4 Middle mouse button

The Shift argument works the in same the same way as in the KeyDown and KeyUp events.  The X and Y arguments return the position of the cursor in relation to the CommandButton i.e. the coordinates of the very top-left of the CommandButton are 0, 0.

The MouseDown event occurs before the Click event and the MouseUp event occurs after the Click event.

Other Things to Try

Coloured Text

It is not possible to change the colour of the text of the CommandButton's text as there is no ForeColor property.  The only way to do it is to create a picture of coloured text and use it as the Picture property.  Remember to set the following properties:

  • Caption: "" (no text)
  • MaskColor: the background colour of the picture if its not already transparent
  • Picture: the picture of the coloured text
  • Style: 1 - Graphical
  • UseMaskColor: True (if necessary)

A CommandButton with coloured text
A CommandButton with coloured text


blog comments powered by Disqus

Archived Comments

From: sachin
Date: Thursday, October 21, 2010 at 10:51:12
Comments: wow wat  a  explaination........

great         very   useful  for  us...

tanking  u........rockz....
 
From: Charlie
Date: Tuesday, March 10, 2009 at 22:48:01
Comments: Thanks for the KeyPress examples, helped clear me up on that.
 
From: Browner87
Date: Saturday, October 4, 2008 at 05:38:48
Comments: To make a command button with colored text, use a checkbox control and set the style to graphical and then it will look just like a command button. THen just use code to uncheck it whenever it's 'checked' (clicked)
 
From: Gireesh
Date: Wednesday, February 6, 2008 at 11:47:58
Comments: Suppose you are trying to trap the combination ALT+A and if there is a button with caption '&Add'. This trap method will not work.
 
From: Jana
Date: Friday, August 31, 2007 at 04:23:23
Comments: Good very useful my project
 
From: RUDRA PRASAD BHAUMIK
Date: Saturday, May 20, 2006 at 09:46:08
Comments: Please can you inform/illustrate more methods other than move and setfocus.
 
Reply: Most of the methods are explained above.  What else did you want to know?

Martin Allen 1999 - 2011.  Last updated Thursday 11 August 2011 07:52:28 PM +0100.