Learn the most popular programming language of the most popular Operating System: Windows


Adding Controls and Event Procedures to Form Modules

So far you have learned how to add controls to a form, but you have not yet done it.  In this chapter, you add controls to form modules and attach event procedures (Visual Basic code) to a control.  How can you do this before you understand the Visual Basic language?  Whether you knew it or not, you wrote some Visual Basic code earlier. Writing and Running Your First Visual Basic Program.”  The Print method, for example, is part of the Visual Basic language.

In this section, you will write several additional Visual Basic instructions.  The instructions take you through the steps slowly to help you gain confidence in working with the language.

You will master several tasks in working through this section, including how to

  • add a control to a form,
  • change the properties of a control,
  • add a procedure to a control,
  • write an assignment statement,
  • use the Dim and remark statements
  • use the Print and Cls methods,
  • assign text to a text box,
  • assign a caption to a label.


The four objectives of this section introduce you to adding controls to a form and writing event procedures to display information:

  1. Understanding standard toolbox control objects
  2. Adding a control to a form and setting its properties
  3. Coding an event procedure for a control
  4. Changing object properties with coded statements

Understanding Standard Toolbox Control Objects

Remember from Running a Visual Basic Program. that a control is an object attached to a form module.  The designers of Visual Basic decided to provide a number of standard controls (located on the toolbox) to represent the standard types of objects to be added to a form.  You can also purchase custom controls, which have an .OCX extension when installed.  To help you remember the differences among controls, tools are grouped into four initial categories.

  1. Text controls
  2. Containers for groups of controls
  3. Button controls
  4. Scroll bar controls

There are still other controls on the toolbox, but the preceding four categories represent a significant number.  Remember that by letting your mouse cursor linger a moment over one of the controls in the toolbox, a ToolTip will appear to identify it.

Text Controls

The following four controls deal primarily with text.  The left-hand side of Figure 3.1 shows how these controls appear on a form.

  • Text Box control – Places text entered by the user on a form.  Information that the user types appears in the text box.  A text box can also contain default text to enable the user to bypass the control if the text does not need to be changed.
  • Label control – Displays text entered by the program designer.  The user cannot type or directly change a label.
  • List Box control – Displays a list of items.  The user can make one or more selections from the list.  With a list box, the Visual Basic designer can add or remove items from a list.
  • Combo Box control – Combines the features of a list box and a text box.  This control gives the user the choice of selecting an item from a list or entering a value from the keyboard.

Two of the text control objects – the text box and the combo box – enable the user to enter data into a program.  On the other hand, a user can make a selection from a list box, but can’t enter data.  Similarly, a label cannot be charged; however, because labels are read-only, they are used often to display the results of processing.


Containers house or contain (like a bucket) a set of controls.  There are two types of container controls, as follows:

  • Picture Box control – Used to store or contain a graphic image, such as a bitmap.  It is typically used with a frame.  It can also serve as a container for other controls, such as a group of images or a toolbar.
  • Frame control – Used to provide a rectangular area on the form where other controls can be placed as a group.  It generally serves as a container for a group of buttons, such as a set of option buttons.

The middle portion of Figure 3.1 shows how these controls appear on a form.


Buttons are controls that users click to turn on or off.  The button manipulation action (for example, a single mouse click) invokes an action an event.  The middle portion of Figure 3.1 shows option buttons, check boxes, a command button, and an Image control.  The following describes the button controls:

  • Option Button control – Enables the user to make one selection from a group of choices, similar to a multiple-choice question.  Several option buttons are usually grouped inside a frame or picture box.  When one option within a group is selected, all other options are canceled.  This is called exclusive choice.  You should use option button when you want the user to make only one selection.
  • Check Box control – Enables the user to turn an option on or off, or set a value to true or false.  Check boxes can also be grouped.  However, unlike option buttons, any number of check boxes can be selected.  This is called non-exclusive choice:  Selecting one does not cancel the others.
  • Command Button control – Places a button on a form that the user clicks to invoke an event.
  • Image control -  Used to place an image on the screen.  A user can click the image like a command button to invoke an event.  Several Image controls, arranged as a group, can be used to design a toolbar.

Scroll Bars

Scroll bars enable the user to examine a list when the entire list does not fit on a screen.  Both horizontal and vertical scroll bars can be added to a form (see the right-handed side of Figure 3.1), as follows:

  • Horizontal Scroll Bar control – Provides for easy navigation through a long line of information, or enables the user to set speed, quantity, or quality.  Placement is across the screen.
  • Vertical Scroll Bar control – Same at the horizontal scroll bar except that placements runs down the screen.

The 12 controls described in this section do not take into account all of the standard controls found in the toolbox.



Popular Posts

Search This Blog

Powered by Blogger.

Featured Post

Coded Statements and Methods

In the preceding procedure , you wrote several coded instructions, which introduced two different programming statements and a method: the D...

Recent Posts