Sie sind auf Seite 1von 6

Creating Controls at Run Time

Control arrays can be created at run time using the statements

 Load object (Index %)

 Unload object (Index %)

Where object is the name of the control to add or delete from the control array. Index %
is the value of the index in the array. The control array to be added must be an element of
the existing array created at design time with an index value of 0. When a new element of
a control array is loaded, most of the property settings are copied from the lowest existing
element in the array.

Following example illustrates the use of the control array.

* Open a Standard EXE project and save the Form as Calculator.frm and save the Project
as Calculater.vbp.

* Design the form as shown below.

Prope
Object Setting
rty
Captio Calculator
n
Form
frmCalcul
Name ator
Captio
1
n
CommandBu
cmd
tton Name
0
Index
Captio
2
n
CommandBu
cmd
tton Name
1
Index
CommandBu Captio 3
tton n
cmd
Name
2
Index
Captio
4
n
CommandBu
cmd
tton Name
3
Index
Captio
5
n
CommandBu
cmd
tton Name
4
Index
Captio
6
n
CommandBu
cmd
tton Name
5
Index
Captio
7
n
CommandBu
cmd
tton Name
6
Index
Captio
8
n
CommandBu
cmd
tton Name
7
Index
Captio
9
n
CommandBu
cmd
tton Name
8
Index
CommandBu Captio 0
tton
n
cmd
Name
10
Index
Captio
.
n
CommandBu
cmd
tton Name
11
Index
Captio
AC
CommandBu n
tton
cmdAC
Name
Captio
+
CommandBu n
tton
cmdPlus
Name
Captio
-
CommandBu n
tton
cmdMinus
Name
Captio *
CommandBu n
tton cmdMultip
Name ly
Captio
/
CommandBu n
tton
cmdDivide
Name
Captio
+/-
CommandBu n
tton
cmdNeg
Name
Name txtDisplay
TextBox
Text ( empty )
CommandBu Captio =
tton n
cmdEqual
Name

The following variables are declared inside the general declaration

Dim Current As Double


Dim Previous As Double
Dim Choice As String
Dim Result As Double

The following code is entered in the cmd_Click( ) (Control Array) event procedure

Private Sub cmd_Click(Index As Integer)


txtDisplay.Text = txtDisplay.Text & cmd(Index).Caption
'&is the concatenation operator
Current = Val(txtDisplay.Text)
End Sub

The following code is entered in the cmdAC_Click ( ) event procedure

Private Sub cmdAC_Click()


Current = Previous = 0
txtDisplay.Text = ""
End Sub

The below code is entered in the cmdNeg_Click( ) procedure

Private Sub cmdNeg_Click()


Current = -Current
txtDisplay.Text = Current
End Sub

The following code is entered in the click events of the cmdPlus, cmdMinus,
cmdMultiply, cmdDevide controls respectively.
Private Sub cmdDevide_Click()
txtDisplay.Text = ""
Previous = Current
Current = 0
Choice = "/"
End Sub

Private Sub cmdMinus_Click()


txtDisplay.Text = ""
Previous = Current
Current = 0
Choice = "-"
End Sub

Private Sub cmdMultiply_Click()


txtDisplay.Text = ""
Previous = Current
Current = 0
Choice = "*"
End Sub

Private Sub cmdPlus_Click()


txtDisplay.Text = ""
Previous = Current
Current = 0
Choice = "+"
End Sub

To print the result on the text box, the following code is entered in the cmdEqual_Click
( ) event procedure.

Private Sub cmdEqual_Click()

Select Case Choice

Case "+"
Result = Previous + Current
txtDisplay.Text = Result
Case "-"
Result = Previous - Current
txtDisplay.Text = Result
Case "*"
Result = Previous * Current
txtDisplay.Text = Result
Case "/"
Result = Previous / Current
txtDisplay.Text = Result
End Select

Current = Result

End Sub

Das könnte Ihnen auch gefallen