Beruflich Dokumente
Kultur Dokumente
0 Page 1 of 7
7th National IT Aptitude For College/Polytech Students & Graduates. Test your IT skills. NIITEducation.com/NITAT
PowerBasic vs VisualBasic Faster. No Run-Times. No Bloat! CGI, Macros, ASM, Reg Expressions www.powerbasic.com
Learn Visual Basic 6 Complete Visual Basic 6 Tutorial. Absolutely Free! visualbasic.freetutes.com
Search
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
http://vb6.martin2k.co.uk/tips/vb_42.php 16/2/2011
The CommandButton Control - Visual Basic 6.0 Page 2 of 7
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.
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:
l Bold
l Charset
l Italic
l Name
l Size
l Strikethrough
l Underline
l 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
http://vb6.martin2k.co.uk/tips/vb_42.php 16/2/2011
The CommandButton Control - Visual Basic 6.0 Page 3 of 7
Index
Left
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.
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:
http://vb6.martin2k.co.uk/tips/vb_42.php 16/2/2011
The CommandButton Control - Visual Basic 6.0 Page 4 of 7
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
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
Methods
Move
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:
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
http://vb6.martin2k.co.uk/tips/vb_42.php 16/2/2011
The CommandButton Control - Visual Basic 6.0 Page 5 of 7
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.
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
When the user presses a keyboard key down or releases a key, these events occur.
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'.
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.
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.:
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
http://vb6.martin2k.co.uk/tips/vb_42.php 16/2/2011
The CommandButton Control - Visual Basic 6.0 Page 6 of 7
MouseDown, MouseUp
This event occurs when a mouse button is pressed down or released over a CommandButton.
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.
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:
Comments
From: sachin
Date: Thursday, October 21, 2010 at 10:51:12
Comments: wow wat a explaination........
tanking u........rockz....
From: Charlie
http://vb6.martin2k.co.uk/tips/vb_42.php 16/2/2011
The CommandButton Control - Visual Basic 6.0 Page 7 of 7
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
Reply: Most of the methods are explained above. What else did you want to know?
Please fill in the below form if you have any comments or additional information you want to add about the above information. If
you have any Visual Basic 6.0 questions, please visit the Forum.
PLEASE NOTE: Questions sent via this form cannot be answered. Please ask questions at the Forum.
* - Mandatory
*Name: E-mail:
*Comments:
*Please type the following code (your comment will not be accepted without it):
PLEASE NOTE: Questions sent via this form cannot be answered. Please ask questions at the Forum.
Send Reset
Martin Allen 1999 - 2010. Last updated Saturday 30 October 2010 02:37:51 PM +0100.
http://vb6.martin2k.co.uk/tips/vb_42.php 16/2/2011