WEEK 9 - WEEK11
Objectives
- To continues for the Visual Basic
Naming Controls
This is the string of characters that
identifies the control in code. This property can't be an empty string, and you
can't have two or more controls on a form with the same name. The special
nature of this property is indirectly confirmed by the fact that it appears as
(Name) in the Properties window.
When to create a control, Visual Basic
assigns it a default name. For example, the first TextBox control that you
place on the form is named Text1, the second one is named Text2, and so forth.
Similarly, the first Label control is named Label1, and the first CommandButton
control is named Command1.
This default naming scheme frees from having
to invent a new, unique name each time you create a control. Notice that the
Caption property of Label and CommandButton controls, as well as the Text
property of TextBox controls, initially reflect the control's Name property,
but the two properties are independent of each other. In fact, just modified
the Caption and Text properties of the controls in the Rectangle Demo form
without affecting their Name properties.
For an example, we will rename the Text1
through Text4 controls as txtWidth, txtHeight, txtPerimeter, and txtArea
respectively. The Command1 control will be renamed cmdEvaluate, and the four
Label1 through Label4 controls will be renamed lblWidth, lblHeight, lblPerimeter,
and lblArea, respectively. However, please note that Label controls are seldom
referred to in code, so in most cases you can leave their names unmodified
without affecting the code's readability.
The control classes and their recommended
prefixes are shown in below Table:
Control Class
|
Prefix
|
Control Class
|
Prefix
|
CommandButton
|
cmd
|
Data
|
dat
|
TextBox
|
txt
|
HScrollBar
|
hsb
|
Label
|
lbl
|
VScrollBar
|
vsb
|
PictureBox
|
pic
|
DriveListBox
|
drv
|
OptionButton
|
opt
|
DirListBox
|
dir
|
CheckBox
|
chk
|
FileListBox
|
fil
|
ComboBox
|
cbo
|
Line
|
lin
|
ListBox
|
lst
|
Shape
|
shp
|
Timer
|
tmr
|
OLE
|
ole
|
Frame
|
fra
|
Form
|
frm
|
Adding Code
This simple program, need to extract the
values stored in the txtWidth and txtHeight controls, use them to compute the
rectangle's perimeter and area, and assign the results to the txtPerimeter and
txtArea controls respectively:
Private Sub cmdEvaluate_Click()
' Declare two floating point
variables.
Dim reWidth As Double, reHeight As Double ' Extract values from input TextBox controls. reWidth = CDbl(txtWidth.Text) reHeight = CDbl(txtHeight.Text) ' Evaluate results and assign to output text boxes. txtPerimeter.Text = CStr((reWidth + reHeight) * 2) txtArea.Text = CStr(reWidth * reHeight)
End Sub
|
No comments:
Post a Comment