Escolar Documentos
Profissional Documentos
Cultura Documentos
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.
Figure 2
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.
Figure 3
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!"
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
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
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).
(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.
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.