Controls
Controls
Controls
Label: Label, the simplest control in the Visual Basic toolbox, displays formatted text on a user interface
form. Typical uses for the Label control include:
Help text
Program splash screen headings
Formatted output, such as names, times, and dates
Descriptive labels for other objects, including text boxes and list boxes.
A label is a control you use to display text that a user can't edit directly.
Label Properties:
Alignment Aligns caption within border.
Appearance Selects 3-D or flat appearance.
AutoSize If True, the label is resized to fit the text specified by the caption
property. If False, the label will remain the size defined at design time
and the text may be clipped.
BorderStyle Determines type of border.
Caption String to be displayed in box.
Font Sets font type, style, size.WordWrap Works in conjunction with
AutoSize property. If AutoSize = True, WordWrap = True, then the text
will wrap and label will expand vertically to fit the Caption. If AutoSize
= True, WordWrap = False, then the text will not wrap and the label
expands horizontally to fit the Caption. If AutoSize = False, the text will
not wrap regardless of WordWrap value.
Label Events:
Click Event triggered when user clicks on a label.
DblClick Event triggered when user double-clicks on a label.
Example:
You can also set label properties with program code, as shown in the following. The program codes
below, when command1 button is clicked, will set the caption of label1 as “Welcome” and label2 as
“Please enter your name below:”
Private Sub Command1_Click ()
Label1.Caption = "Welcome"
Label2.Caption = "Please enter your name below:"
lblDigitalClock.Caption = Time
End Sub
Text Box:
A Textbox is used to display information entered at design time, by a user at run-time, or assigned within
code. The displayed text may be edited. The Textbox control is one of the most versatile tools in the
Visual Basic toolbox. This control performs two functions:
Displaying output (such as operating instructions or the contents of a file) on a form.
Receiving text (such as names and phone numbers) as user input.
How a text box works depends on how you set its properties and how you reference the text box in your
program code. The following popup illustration shows a text box that:
1
Displays data entry instructions for the user.
Receives input when the user types in a delivery note and clicks the Print Order button.
Example: The sample code below shows how to control a text box, Command 1 is clicked,
Private Sub Command1_Click()
'The following line places text in a text box:
Text1.Text = "Enter your delivery note here."
End Sub
Command 2 is clicked,
Private Sub Command2_Click()
'This line saves input from a text box in a variable:
DeliveryNote = Text1.Text
End Sub
Command Button
A command button is the most basic way to get user input while a program is running. By clicking a command
button, the user requests that a specific action be taken in the program. Or, in Visual Basic terms, clicking a
command button creates an event, which must be processed in your program.
Here are some command buttons that you’d typically find in a program:
OK Accepts a list of options and indicates that the user is ready to proceed.
Cancel Discards a list of options.
Quit Exits an open dialog box or program.
In each case, you should use command buttons carefully so that they work as expected when they are clicked.
2
Command Button Properties:
Appearance Selects 3-D or flat appearance.
Cancel Allows selection of button with Esc key (only one button on a form can have
this property True).
Caption String to be displayed on button.
Default Allows selection of button with Enter key (only one button on a form can have
this property True).
Font Sets font type, style, size.
Command Button Events:
Click Event triggered when button is selected either by clicking on it or by pressing
the access key.
Example:
Option Button: To receive user input in a Visual Basic program, you can use controls that:
Present a range of acceptable choices.
Receive input in an acceptable format.
The Option Button control satisfies these requirements. It displays a list of choices on a form and requires the user
to pick one (but only one) item from a list of choices.
Creating Option Buttons
Like the other items in the Visual Basic toolbox, you create an option button on a form by clicking Option Button
in the toolbox and then drawing on the form.
Grouping Option Buttons
If you want to create a group of option buttons that work together, you must first create a frame for the buttons. (To
do this, use Frame, a Visual Basic toolbox control.) Next, place your option buttons inside the frame so that they
can be processed as a group in your program code and moved as a group along with the frame. See figure below.
3
Checkbox
The Checkbox control is similar to the OptionButton control, except that Checkbox displays a list of choices and
gives the user the option to pick multiple items (or none at all) from a list of choices.
You create a check box on a form much as you would make an option button. Start by clicking the Checkbox
control in the toolbox, and then draw on the form. If you want to create a group of check boxes that work together,
create a frame for the buttons with Frame, a toolbox control. Then, you can place your check boxes inside the frame
so that they can be processed as a group in your program code or moved as a group along with the frame. See figure
below.
ListBox
When you want a user to choose a single response from a long list of choices, you use the ListBox control. (Scroll
bars appear if the list is longer than the list box.) Unlike option buttons, however, list boxes don’t require a default
selection.
4
Creating List Boxes
Building a list box is simple, since it doesn’t require creating a frame or a separate object for each list item. You just
click ListBox in the Visual Basic toolbox and draw a suitably sized list box on your form.
Setting List Box Properties
In a Visual Basic program, you can define list box characteristics in the Properties window or by using program
code, so that users can add, remove, or sort list box items while the program runs.
You add choices to a list box with the AddItem method, which is typically placed in the Form_Load event
procedure. (AddItem is a method normally used with list-oriented objects).
The sample code below shows an example of the AddItem method.
Private Sub Form_Load()
List1.AddItem “Extra hard disk”
List1.AddItem “Printer”
List1.AddItem “Satellite dish”
End Sub
ComboBox
Objects created with the ComboBox control are similar to those created with the ListBox control. Like a list box,
items can be added to, removed from, or sorted in a combo (combination) box while your program runs. However,
there are differences. Combo boxes take up less space on the form, show a default setting, and in their default Style
property setting are only one line high. But if the user clicks the combo box while your program runs, the combo
box expands to reveal a full list of choices. (If the list is too long to be displayed in the combo box, scroll bars
appear).
5
Adding Combo Box Items
Typically, choices are added to a combo box with the AddItem method (a method normally used with list-oriented
objects). You use the AddItem method by writing code in the Form_Load event procedure.
The sample code below shows an example of the Form_Load event procedure.
Private Sub Form_Load()
Combo1.AddItem “U.S. Dollars”
Combo1.AddItem “Check”
Combo1.AddItem “English Pounds”
End Sub
Exercise:
6
WORKING WITH MENUS AND DIALOG BOXES
Menus, which are located on the menu bar of a form, contain a list of related commands. When you click
a menu title in a Windows-based program, a list of menu commands should always appear in a well-
organized list.
Using the Menu Editor:
The Menu Editor is a Visual Basic dialog box that manages menus in your programs. With the Menu
Editor, you can:
Add new menus
Modify and reorder existing menus
7
Delete old menus
Add special effects to your menus, such as access keys, check marks, and keyboard shortcuts.
See the Figure below for Menu editor Window
9
Processing Menu Choices
After you place menu items on the menu bar, they become objects in the program. To make the menu
objects do meaningful work, you need to write event procedures for them. Typically, menu event
procedures:
Contain program statements that display or process information on a form.
Modify one or more object properties.
For example, the event procedure for a command named Time might use the Time keyword to display
the current system time in a text box.
Processing the selected command might require additional information (you might need to open a file on
disk, for example). If so, you can display a dialog box to receive user input by using a common dialog
box.
Disabling a Menu Command
In a typical Windows application, not all menu commands are available at the same time. In a typical Edit
menu, for example, the Paste command is available only when there is data on the Clipboard. When a
command is disabled, it appears in dimmed (gray) type on the menu bar. You can disable a menu item by:
Clearing the Enabled check box for that menu item in the Menu Editor.
Using program code to set the item's Enable property to False. (When you’re ready to use the
menu command again, set its Enable property to True.)
This table lists the name and purpose of the six standard dialog boxes that the common dialog object provides and
the methods you use to display them:
Dialog Box Purpose Method
Open Gets the drive, folder name, and file name ShowOpen
for an existing file that is being opened.
Save As Gets the drive, folder name, and file name for a file that is ShowSave
being saved.
Print Provides user-defined printing options. ShowPrinter
Font Provides user-defined font type and style options. ShowFont
Help Provides online user information. ShowHelp
Color Provides user-defined color selection from a palette. ShowColor
12
Basic to display a standard palette of color selections — is a hexadecimal (base 16) number. (To see a list of other
potential values for the Flags property, search for CommonDialog constants in the Visual Basic online Help.) The
event procedure assumes that you have already created a menu command named TextColor with the Menu Editor.
See the code given below
Private Sub mnuTextColorItem_Click()
CommonDialog1.Flags = &H1&
CommonDialog1.ShowColor
Label1.ForeColor = CommonDialog1.Color
End Sub
The figure below shows the color dialog box.
14