www.hitmill.com
link to hitmill.com home page
 

 

Visual Basic
Command Control Button

The Command button is a common control frequently used in VB programming. This is one of the first controls that beginners learn to place onto a form and then to code an "event procedure" for the command control object. An event procedure is something that happens in response to a user action such as a click or a "mouse over". (If an object on a form has no corresponding code written by the programmer, nothing will happen if the user clicks on the control or attempts any other action with that control.)

 
Bitmap:Command control button  
Double-clicking on a Command button in the toolbar will place it onto the center of your form. Left click on it the "draw" it onto the form by dragging the mouse and at the same time, holding down the left mouse key while dragging. Release the left mouse key when the object is the size you want it to be. You can also "drag" an object in the work area by first selecting the object with a single mouse click then dragging the object by positioning the mouse pointer over the object, holding down on the left mouse key, and moving the object to a new location in the work area, then releasing the left mouse button to stop the move and finally, by clicking somewhere off the object to deselect the object that was just moved.
While the mouse pointer is over the Command button, click and release the left mouse button to highlight (select) the Command button. You will see resizing handles appear which you can drag diagonally, horizontally, or vertically to change the size of the Command button. The only reason you would want to do this is to fit a large caption onto the button. Most of the time, you will not need to resize the Command button. Beginners like to play with the resizing so have fun with it.
Another way to place a Command button onto a form is to single-click on the Command button in the Toolbar. Then place the mouse pointer over the form and hold down on the left mouse button while dragging a short diagonal line from corner to corner where you want the button to be placed. If the size is not right, left-click on the button and drag the re-sizing handles to make it the size you need.

The Name and Naming Conventions

Getting down to serious business, the Command button will need to have a name. It is named while it is highlighted (selected) on the form. While the Command button on the form is selected, go to the Properties Window and select the Alphabetical tab. At the top of this list the first item to appear will be Name. Click on Name and to the right of that, double click in the Name space to highlight the words Command1. Start typing a name for your Command button beginning with the lowercase letters "cmd" without the quotation marks, just plain cmdName where Name is the one you select, such as cmdShow3.
Naming conventions for controls and variables (textbook naming conventions for first and second semester VB students) are discussed in the article Hungarian Notation. Many programmers begin the name part (after the cmd prefix) with a capital letter to make it easier to read, such as cmdLarge. If two words are to be in the name of any control such as a Command button, be sure you do not allow any spaces in the name. Begin the second word with a capital letter like this: cmdSecondWord. This is only done to make it easier to read. cmdsecondword is also legal but is harder to read. CASE (Uppercase or lowercase) does not matter. THIS is the same word as this. Visual Basic will revert back to whatever uppercase and lowercase letters were used in a variable at the time you write the Dim statement to dimension the variable. More on this latter. A total of 255 characters are permissible in a control or variable name. Technically, after the variable name begins with at least one letter, any combination of letters, numerals, and underscores may be used. Special characters like period or ampersand (&) are not allowed and all students are encouraged by programmers to please use the Hungarian notation conventions when naming controls and variables.
The Hungarian Notation prefixes (again, for VB students) are used for ease in debugging, to help you remember what type of variable or control you made (after a long time has elapsed you may not remember). Hungarian notation has been accepted by programmers as a type of naming convention or standardization in coding and in naming controls and variables. Many times programmers work on modules -- little pieces of larger programs and many people work on one program. Without the Hungarian Notation protocols your own variables would make less sense to another person. The prefix tells another person what type of control or variable you have created.

The Caption: And Using the ampersand (&) in a Command button Caption

Caption is a Property of the Command control button (the command button). Left click on the Command button again on the form, to select it. Return to the Properties window. Click on the Alphabetical tab. Scroll down until Caption is read and click on it. Double click on the words to the right of Caption and start typing the words you want to appear on your button.
For an exit button type E&xit for the Caption and touch ENTER. Why the ampersand? You can use an ampersand (&) one time in each Control button Caption. This will underline one letter in the caption. It is the letter following the ampersand that will be underlined. That underlined letter signifies which corresponding keyboard letter key can be used along with the ALT key to access the Control button. By using the ALT key (held down) while pressing the corresponding letter key on the keyboard you would get the same program output as if you had "pressed" on a Command button by clicking on it with the mouse instead. To rephrase this: if the exit button has a Caption of E&xit, the letter x is underlined then you could press ALT + x to exit the program instead of having to click on the exit button.

The Default Property of the Command control button

This is a nice property to set at design time. The only two possible values are Default = False or Default = True. Only one command button per form should have a Default = True setting. With Default = True set for a particular Command button, then that button's click_event can be achieved by pressing the ENTER key instead of having to use the mouse. If you do not do anything at all with this Default property, Visual Basic will always set the Default = False option. This is convenient in that most all Command buttons will need to have a false Default setting since only one button with the true Default setting can be accessed with the ENTER key (per form). In the Properties window (F4 shortcut to the properties window), scroll down to Default and click on the dropdown arrow to see two choices. Click on True, if changing the Default to true. Do nothing and it will be left at the Default = False, automatically.

The Cancel Property of the Command button

The Cancel Property determines whether or not a Command button is the cancel button of the form, the button whose action of canceling a form is accessed by the Escape key (ESC on the keyboard). Visual Basic by default sets Escape = False for all command buttons, since only one Command button per form can be accessed by the ESC key (escape). I use the Exit button if I have one on the form and at design time in the Properties Window I set the Cancel option to True. To do this, select the Command button which is to be the cancel button. Scroll in the Properties Window to the Cancel property. Click on the drop down arrow and click on True. Touch ENTER key. (Just like the Default property there are only two settings for the Cancel property, True or False.). Again, do nothing and the setting will automatically be False.
Now, a quick recap: There are three ways for a user to access the cmdExit (command button):
  • Click on the button
  • Use the keyboard short-cut, ALT + X, if the programmer has used E&xit in the Caption so that X will be a "hot key" to use with ALT key.
  • Use the ESC key (Escape), if the Cancel Property of cmdExit has been set to True by the programmer (at Design time).
Note:  An additional way to exit or cancel the form from Run mode is the obvious Windows method of clicking on the exit button in the Title bar of the form.

ToolTips Property for any control object, including the Command button

The Cancel Property and the Default Property were inherent only to the Command button. Many properties available to the command button may also be set with other control objects from the toolbar, and even objects not from the toolbar. The ToolTips property is available for setting with any of the Visual Basic control objects.
The ToolTips property is a clever way to have the user read a message about a control object simply by hovering with the mouse pointer over that object for a couple of seconds.
Setting the message of this property is very simple. Just click on the Command button or another object to select it and then in the Properties Window (F4), scroll down to ToolTips and double click in the space to the right of ToolTips and start typing your message without quotation marks. You will need to run the program (F5 shortcut), and hold your mouse pointer over the object in order for your ToolTip message to appear. Always check your ToolTips in the Run mode just to make certain they are functioning the way you want them to. Remember, set this Property, preferably at Design time.


webmaster@hitmill.com
 
Updated -

© hitmill.com
All Rights Reserved