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 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.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
Command1.Font.Bold = True Command1.Font.Italic = True Command1.Font.Underline = True |
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).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 IntegerWhen 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 |
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 IntegerThis 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 SingleThis 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 |
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)
Post a Comment