|
|
Beginner TipThe 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. PropertiesBackColorSets the colour of the CommandButton's background. This property only takes effect when the Style property is set to 1 - Graphical. CancelIf 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. CaptionSets 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. DefaultWorks 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. DisabledPictureSets 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. DownPictureSets 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. EnabledWhen 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. FontIn 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:
For example:
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. HeightSets the height of the CommandButton. IndexLeave blank unless the CommandButton is to be part of a control array. LeftSets the X coordinate of the CommandButton. MaskColorThis 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. MouseIconWhen 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. MousePointerSets the type of mouse pointer to be used when the user hovers the mouse over the CommandButton. The possible choices are:
PictureSets 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. StyleThis needs to be set to 1 - Graphical if you want to use the graphical properties of the CommandButton (apart from Fonts). TabIndexSets 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. TabStopIf you set this to False, the CommandButton will not get focus when the tab key is pressed. TagThis property can be set to a string if you need to store any extra information related to the CommandButton. ToolTipTextWhen 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. TopSets the Y coordinate of the CommandButton. UseMaskColorIf 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. VisibleDetermines 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. WidthSets the width of the CommandButton. MethodsMoveLeft 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:
SetFocusSets 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. EventsClickThis 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.
GotFocusOccurs 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, KeyUpKeyCode As Integer, Shift As Integer 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.
KeyPressKeyAscii 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. LostFocusOccurs when the CommandButton loses focus. MouseDown, MouseUpButton 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.
The Button argument returns one of the following possible values:
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 TryColoured TextIt 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
Martin Allen 1999 - 2007. Last updated Sunday 23 December 2007 12:36:34 AM -0000. |