Você está na página 1de 7

Visual Basic Controls

? Visual Basic Controls


Controls are the elements of the User Interface with which the user can interact and
control the application. Form is the first and main element of the user interface which acts
as container for other elements. The basic controls are displayed as icons on the toolbox
which is a part of the interface during the design time.

The default icons with their names is given as below:

Pointer Picture Box

Label Text Box


Frame Command
Check
Radio Button
Combo List Box
Horizontal Vertical
Timer Drive List Box

Directory List Box File List Box


Shape Line

Image Control Data (DAO)


OLE

Training Division, NIC, New Delhi


C 1
Visual Basic Controls

? Standard Visual Basic Controls

Control Control Name Name Function Example


Button Prefix
CheckBox chk Displays or enables input of Assignment 2 in
a two-part choice, such as VB Controls
Yes/No or True/False.
ComboBox cbo Enables the user to select an Assignment 3 in
entry from a list or enter a VB Controls
new value.
CommandButton cmd Enables the user to initiate a Assignment 1 in
program action. Can VB Controls
include an icon, caption,
and ToolTips.
Data Control dat Provides a link to database
files.
Dir ListBox dir Displays and enables a user Assignment 1 in
to choose from available VB Programming
subdirectories on a drive.
DriveListBox drv Displays and enables a user Assignment 1 in
to choose from available VB Programming
disk drives on the
computer.
FileListBox fil Displays and enables a user Assignment 1 in
to choose from available VB Programming
files in a directory.
Frame fra Serves as a container for Assignment 2 in
other controls. Provides a VB Controls
method for grouping
controls. (To group controls
in a frame, select the frame
with a single-click first,
then draw a control in it.)
HscrollBar hsb Produces a numerical value
(Horizontal based on the scroll bar's
Scroll bar) horizontal position.
Image img Displays a graphic image. Assignment 1 in
Similar in appearance to the VB Programming
picture control but with
different functionality.
Label (lbl) lbl Displays text that the user Assignment 4 in

Training Division, NIC, New Delhi


C 2
Visual Basic Controls

cannot directly modify. VB Controls


Line lin Displays lines on the form.

ListBox lst Displays a list of items Assignment 3 in


from which the user can VB Controls
select one or more entries.
OLE Container ole Provides you with a way to Assignment 4 in
link to OLE servers. VB Controls
OptionButton opt Displays or enables a Assignment 2 in
choice among multiple VB Controls
items. (Also known as a
radio button.)
PictureBox (Pic) Pic Displays a graphic image. It
can also serve as a
container for other controls
Shape shp Displays geometric shapes
on the form.
TextBox txt Displays text that the user Assignment 1 in
can edit. VB Controls
Timer tmr Provides a means for an Assignment 5 in
action to be taken after VB Controls
passage of a certain amount
of time.
VscrollBar vsb Same as above but vertical.
(Vertical Note the scrollbars behave
Scroll bar) like standard Windows
scrollbars.

? Placing a control on a form

To place a control into use, select the control from the toolbox. The mouse pointer
should change to a placement icon when placed over a form. Select the location to place
the control and either click on the form, or click on a location and drag the mouse to
adjust the controls visible size.

Note- Not all controls are re-sizable.

Training Division, NIC, New Delhi


C 3
Visual Basic Controls

Figure 2

? Changing control properties

Each control has a set of values associated with it known as its properties. These
properties describe how the control will appear on a form. For instance, a textbox control
will have a property called text which describes the contents of the textbox.
To change the properties for a given control, click on the control to select it.
If the properties window is visible, it will update to list the properties associated with the
selected control and the current value of each. If the properties window is not visible,
select View->Properties from the menu.
In the properties window, select the property that you wish to change and edit the current
value directly in the window.

Note: Some controls have properties that will not appear in the properties window.
These are called run-time properties and can only be changed through code.

Training Division, NIC, New Delhi


C 4
Visual Basic Controls

Figure 3

? Setting control properties from code

Just as you changed control properties from the properties window, you can modify them
through code. Locate a position in your code where you want to modify a controls
properties. Insert a line of code with the following syntax:

control_name.propertyname = new_value

Where control_name is the name of your control, property_name is the name of the
property that you wish to modify, and where new_value is the value you wish to set.

Note- Remember to use the period (.) to separate the control from the property.
example: Text1.Text = "Hello World!"

? Retrieving a property value

The process of retrieving a controls property value in code is much like setting the value
in code.
example: variable = Text1.TextThis example retrieves the value of the text property
from the control Text1 and places the value into the variable

Training Division, NIC, New Delhi


C 5
Visual Basic Controls

? Setting the tab order for controls

When your program runs, the user can navigate through each control using the Tab key.
You can set the tab order for each control capable of receiving focus so as to create a
structured path for the user to follow.

To set a controls position in the tab order, change the controls tabindex property to an
integer number designating its placement in the order. The Windows tab order runs from
0 (first) to n (last).

Tip: Start with the last control on your form and set its tabindex property to 0. Click on
the next to last control. The properties window should now be waiting for the tabindex of
that control. Set it to 0. Each time you assign a control a tabindex of 0 (or any number)
Visual Basic automatically increments (renumbers) those controls with an assigned
tabindex greater than or equal to the index entered. So the last control would be
renumbered to tabindex 1

? Default property Of A Control

Every control has a default property, which is usually the most used propert y of that
control. For instance, the default property of the label control is the .caption property, for
the text box it is the .text property, and for the image control it is the .picture property.
This means that you don't have to type out the full control name and property for each
control. The following pair lines of code have identical effects.

textbox.text = "text"
textbox = "text"

label.caption = "caption"
label = "caption"

image.picture = LoadPicture(picture.bmp)
image = LoadPicture(picture.bmp)

? Control Array
Suppose you have a form with 100 command buttons and they all accomplish pretty
much the same task -- they print their caption to a label control. Now what would be
better; to draw 100 command buttons and name them btnPrint1 through btnPrint100, or
should you find a better way to do it? In this scenario you would undoubtedly use a
control array.

A control array is exactly what the name implies -- an array of controls. So instead of
100 different names, you would have btnPrint(1) through btnPrint(100).

Training Division, NIC, New Delhi


C 6
Visual Basic Controls

? Setting up a Control Array

(i) In a control array, there is one control which the other controls take after when
they are created. Each control in a control array also has an index so we can
determine which one is to be used. You assign the index of each control by
giving the control a value in it's .index property. If a control has a value in it's
.index property, then it is assumed by the program to belong to a control array.
(ii) Every control in the control array also has the same name, and they are
distinguished as different contro ls through their index property. Let's set up a
program with 5 command buttons that are part of a control array. When you
click on one of the 5 buttons a notice will be displayed in a label indicating
which button has been clicked.

Object Property & its Values

button .name=btnControl .index=0 .caption="Button &1"


button .name=btnControl .index=1 .caption="Button &2"
button .name=btnControl .index=2 .caption="Button &3"
button .name=btnControl .index=3 .caption="Button &4"
button .name=btnControl .index=4 .caption="Button &5"
label .name=lblNotice .caption=""

Notice that the indexes start from 0 and go to 4, but the captions indicate the button's
number is one greater than it's index. This is taken into account when we write code
for the controls.

(iii) All the controls in a control array share the same code, and each event of a
control in a control array is passed the control's index. So this index argument
can be used in the code, as is shown below:
Sub btnControl_Click (Index As Integer)
lblNotice = "You selected button " & Str$(Index + 1)
End Sub

Note: We're just setting the caption of lblNotice to indicate which button we
pushed. We add one to Index because the first index is 0, and the caption of the first
button indicates that it is number 1. So we take this into account by adding one to the
index.

Training Division, NIC, New Delhi


C 7

Você também pode gostar