Home

Primary Controls

 

Microsoft Visual Basic

 

Command Buttons

A command button is a control that allows the user to make a decision based on what the form is displaying. This could be a simple acknowledgement such as a form displaying a quick message to the user. It could also require the user to make other selections or perform some settings on a dialog box before continuing to other issues.

As a programmer, the computer and the user count on you to decide on the role of a button. And as stated already, a button can assume any role you assign it to.

 

Practical Learning: Inserting A Command Button

  1. On the Toolbox, double-click the CommandButton
  2. On the Properties window, make the following changes:
     
    (Name) cmdEmployees Left 120
    Caption Employees Top 3120
    Height 375 Width 1215
  3. To test the form, press F5 (of course, the button doesn't do much because we didn't ask it to do anything).
  4. There are many ways you can access the area where you will be writing code. Usually, the code you write will be associated with one of the controls on your form. Although many behaviors are associated with each control, every control, including the form itself, has a default behavior. For example, the default behavior of a button is the click action.
    To access the code associated with the first button, double-click Employees.
    This opens the Code Editor window.
  5. Notice that the mouse cursor is inside of the click event code associated with the cmdEmployees button.
    Change the code of the event as follows:
    Private Sub cmdEmployees_Click()
        frmEmployees.Show
    End Sub
  6. To test the effect of the new code, on the Standard toolbar, click the Start button
  7. When the main form is loaded, click the Employees button. Notice that the second displays.
  8. To close the Employees Records form, click its Close button
  9. Notice that the caption did not change on the main form, although it would change if you had double-clicked inside of the form.
  10. To close the main form, click its Close button .
  11. On the Toolbox, double-click CommandButton.
  12. On the Properties window, make the following changes:
     
    (Name) cmdExit Left 3480
    Caption Exit Top 3120
    Height 375 Width 1215
  13. Earlier you saw that when a form is loaded, if you click the End button on the Standard toolbar, the form is closed. Now, we will write code that will tell Microsoft Visual Basic that when we click the Exit button, we want it to behave as if we had clicked the End button on the Standard toolbar.
    On the main form, double-click the Exit button.
  14. Press Tab and type End so your code appears as follows:
    Private Sub cmdExit_Click()
        End
    End Sub
  15. To test the functionality of the form as this time, on the Standard toolbar, click the Start button
  16. Click the Employees button
  17. To close the Employees Records form, click its close button .
  18. To close the main form, click Exit.
  19. To save the project, on the main menu, click File -> Save Project

 

Labels

A label is a control used to display text on a form. This control is for two main purposes: to provide quick information or to help the user identify another control on the form.

The properties of a label are completely under your control; the user cannot manipulate them. Although you can change these properties at will, make sure you set their appropriate values in the Properties window. Always avoid distracting the user.

 

Practical Learning: Creating A Label

  1. To create a new project, on the main menu, click Filed -> New Project
  2. On the opening dialog box, make sure Standard EXE is selected and click Open.
  3. On the Toolbox, click Label.
  4. On the form, draw a rectangular box from the top left section to the right section.
  5. On the Properties window, click (Name) and type lblFormTitle
  6. Click the Alignment field, click the arrow of its combo box and select 2 - Center
  7. Click Caption and type Rockville Auto-Mart
  8. Click the Font field. Click its Build button to call the Font dialog box.
  9. On the Font dialog box, set the font to Times New Roman, Style = Bold, and Size = 22.
  10. Click Height and type 495
  11. Click Left and type 120
  12. Click Top and type 120
  13. Click Width and type 4500
  14. To test the form, press F5
  15. To close the running form, click its close button .
  16. On the Toolbox, double-click Label.
  17. Using the label's handles, move it under the main title and on the left.
  18. By double-clicking and moving the controls, create the following four labels:
     
    (Name) lblFirstName lblLastName lblFullName lblMessage
    Caption First Name LastName Full Name Empty
    Height 255 255 255 255
    Left 120 120 120 120
    Top 705 1125 1560 2160
    Width 855 855 855 3975
  19. To test the form, press F5.
  20. After viewing the form, click Close button .

Text Boxes

A text box is used to receive information from the user by her typing it into this box. A text box can also be used to simply display information to the user.

Although a text box is a good control by itself, it usually doesn't mean much to the user if she doesn't know what the box is used for. Therefore, you should always create a text box in conjunction with a label. The label is used to identify what the text is used for. You should position the label on the left or on top of the text box. The rules of using the computer suggest that a text box be identified by the caption of its label. For example, if a label displays First Name:, the text box on its right would be called The First Name Text Box. If a labels displays How Old Are You?, its corresponding text box would be called The How Old Are You Text Box. But remember, programmatically, the text box and its label are completely independent.
 

Practical Learning: Adding A Text Box

  1. On the Toolbox, double-click TextBox.
  2. On the form, drag the text box up a little bit to move it up.
  3. By double-clicking, add three more boxes.
  4. On the Properties window, make the following changes
     
    (Name) txtFirstName txtLastName txtFullName
    Height 285 285 285
    Left 1080 1080 1080
    Text Empty Empty Empty
    Top 705 1125 1560
    Width 1815 1815 1815
  5. To test the form, press F5.
  6. To close the running form, click its close button .

Introduction To Expressions

The contents of controls are worth nothing unless you put them to good use. This is done through expressions.

An expression is a combination of values and signs that allow you to get a new value to display in a control. This is handy because some values will simply not be available when the user is using your program. For example, to get a sum of two numbers that the user is supposed to enter in a form, when creating such a form, you cannot know what the user would enter in two boxes, but you can write an expression that would use whatever the user enters, to get a new value. The values involved in an expression could be external to the program, such as algebraic numbers; they could also come from the program itself, such as BillingRate or DateHired; and they could be a combination of algebraic numbers and controls content on the your program.

An operation, an expression, are the result of a function is usually meant to be displayed in a new and appropriate control.

 

Option Buttons

Sometimes referred to as radio buttons, option buttons allow the user to select one item in a group.

We have already seen that controls are placed on a form before being used in an application. The controls on a form are considered in a group. A frame allows you to create a group in a group. This is particular useful when you want to use option buttons in your program. You should create a group of these buttons so that the user can select one.

 

Practical Learning: Creating Option Buttons

  1. On the Toolbox, click the OptionButton control.
  2. On the form, click and drag inside of the frame. For this exercise, you don't need to be precise when dragging but make sure you draw only inside of the frame.
  3. On the Properties window, change the name of the option button to optFord and change its caption to Ford
  4. From the Toolbox, draw another OptionButton inside of the frame but under the first option button.
  5. Change the name of the new option button to optHonda and its caption to Honda
  6. From the Toolbox, draw another check box inside of the frame but under the second option button.
  7. Change the name of the new option button to optBMW and its caption to BMW
  8. To test the form, press F5
  9. To close the running form, click its close button .

Check Boxes

Check Boxes allow to user to make selections by click these boxes. The user knows that a choice is made when a check mark appears inside of the corresponding square. Programmatically, a check box specifies a value as being true or false.

Although they are usually treated as a group, the user can select more than one check box or as many as are available. As you will see in your programming experience, the functionality of check boxes can be very easy or very complex, depending on the result you are trying to achieve.

 

Practical Learning: Creating Check Boxes

  1. On the Toolbox, click Frame
  2. On the form, click on the right section of the form but outside of the existing frame and drag right a little bit.
  3. On the Properties window, make the following changes:
     
    (Name) fraFord Top 720
    Caption Ford Visible True
    Height 1575 Width 2055
    Left 2280    
  4. On the Toolbox, click the CheckBox.
  5. On the form, click and drag inside of the right frame. For this exercise, you don't need to be precise when dragging but make sure you draw only inside of the right frame.
  6. On the Properties window, change the name of the check box to chkFocus and change its caption to Focus.
  7. From the Toolbox, draw another check box inside of the left frame but under the first check box.
  8. Change the name of the new check box to chkEscort and its caption to Escort
  9. From the Toolbox, draw another check box inside of the left frame but under the second check box.
  10. Change the name of the new check box to chkTaurus and its caption to Taurus
  11. To test the form, press F5
  12. To close the running form, click its close button
 

Previous Copyright © 2001-2004-2014 FunctionX, Inc. Next