Sie sind auf Seite 1von 34

DEPARTMENT OF TECHNICAL EDUCATION

NDHRA PRADESH

Name of the faculty : G. Radha


Designation : Lecturer
Branch : CCP
Institute : 026 - SG Govt. Polytechnic, Adilabad
Year / Semester : III Year / V Semester
Subject : Visual Basic – I
Subject Code : CCP - 503
Topic : Understand designing the user
Interface
Duration : 50 Mts.
Sub Topic : Control Arrays
Teaching Aids : Animations & Images
CCP-503.10 1
Recap

 Common properties of the Controls

 Enabling and disabling the Controls

CCP-503.10 2
Objectives :
Upon the completion of this period the student
will be able to

 Know about the Control Arrays

CCP-503.10 3
Control Array

 It is a group of Controls that share the same name


and type
 The main advantages of adding Controls with
control arrays are
 it uses less memory resource than adding
multiple Controls of same type at design time
 All the controls of the array can share same
event procedures
CCP-503.10 4
Creating Control Array at Design Time
There are three ways to create a control array at
design time.
 Assigning the same name in the Name property for
more than one Control
 Copying an existing Control and then pasting it
onto the Form

 Setting the Index property to a value that is not null

CCP-503.10 5
Adding a Control Array at Run Time

Control arrays can be created at run time


using the following 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

CCP-503.10 6
Control Array application
This application uses a set of Command Button
Controls in an array to illustrate the working
of a Calculator.
 A standard EXE Form is created and saved
 The Form is designed as per the following
specifications
Note: This application may be explained after
covering the programming part

CCP-503.10 7
Control Array application (Calculator)

Object Property Settings


Command Button Name Cmd
Caption 1
Index 0
Command Button Name Cmd
Caption 2
Index 1

Command Button Name Cmd


Caption 3
Index 2
CCP-503.10 8
Control Array application (Calculator)
Object Property Settings
Command Button Name Cmd
Caption 4
Index 3
Command Button Name Cmd
Caption 5
Index 4

Command Button Name Cmd


Caption 6
Index 5

CCP-503.10 9
Control Array application (Calculator)

Object Property Settings


Command Button Name Cmd
Caption 7
Index 6
Command Button Name Cmd
Caption 8
Index 7

Command Button Name Cmd


Caption 9
Index 8
CCP-503.10 10
Control Array application (Calculator)

Object Property Settings


Command Button Name Cmd
Caption 0
Index 9
Command Button Name Cmd
Caption .
Index 10

Text Box Name Text1


Command Button Name CmdAC
Caption AC

CCP-503.10 11
Control Array application (Calculator)
In addition to these Controls, six more
Command Button controls are added with the
following specifications.
Object Property Settings
Command Button Name CmdPlus
Caption +
Command Button Name CmdMinus
Caption -
Command Button Name CmdDiv
Caption /

CCP-503.10 12
Control Array application (Calculator)

Object Property Settings


Command Button Name CmdMul
Caption *
Command Button Name CmdNegative
Caption +/-
Command Button Name CmdEqual
Caption =

CCP-503.10 13
Control Array application (Calculator)

CCP-503.10 14
Control Array application (Calculator)

Declare the following variables in the


General Declarations of the Form.

Public Curval as Double


Public Preval as Double
Public Choice as String
Public Result as Double

CCP-503.10 15
Control Array application (Calculator)

Enter the following code in Cmd_Click() (Control


Array) event procedure.

Private Sub Cmd_Click(Index As Integer)


Text1.Text = Text1.Text + Cmd(Index).Caption
Curval = Val(Text1.Text)
End Sub

CCP-503.10 16
Control Array application (Calculator)

Enter the following code in the CmdAC_Click()


event

Private Sub CmdAC_Click


Curval = Preval = 0
Text1.Text = “ “
End Sub

CCP-503.10 17
Control Array application (Calculator)

Write the following code in the


CmdNegative_Click() event

Private Sub CmdNegative_Click


Curval = -Curval
Text1.Text = Str(Curval)
End Sub

CCP-503.10 18
Control Array application (Calculator)
Enter the following code in the Click event of
the CmdPlus, CmdMinus, CmdMul, CmdDiv
controls respectively.

Private Sub CmdPlus_Click


Text1.Text = “ “
Preval = Curval
Curval = 0
Choice = “+”
End Sub

CCP-503.10 19
Control Array application (Calculator)

Private Sub CmdMinus_Click


Text1.Text = “ “
Preval = Curval
Curval = 0
Choice = “-”
End Sub

CCP-503.10 20
Control Array application (Calculator)

Private Sub CmdMul_Click


Text1.Text = “ “
Preval = Curval
Curval = 0
Choice = “*”
End Sub

CCP-503.10 21
Control Array application (Calculator)

Private Sub CmdDiv_Click


Text1.Text = “ “
Preval = Curval
Curval = 0
Choice = “/”
End Sub

CCP-503.10 22
Control Array application (Calculator)
Enter the following code in the
CmdEqual_Click() event to print the result in
the Text Box
Private Sub CmdEqual_Click()
Select Case Choice
Case "+"
Result = Preval + Curval
Text1.Text = Str(Result)
Case "-"
Result = Preval - Curval
Text1.Text = Str(Result)
CCP-503.10 23
Control Array application (Calculator)

Case "*"
Result = Preval * Curval
Text1.Text = Str(Result)
Case "/"
Result = Preval / Curval
Text1.Text = Str(Result)
End Select
Curval = Result
End Sub

CCP-503.10 24
Control Array application (Calculator)

Save and run the project.

On clicking digits of user’s choice and an


operator button, the output appears in the text
box as shown below.

CCP-503.10 25
Control Array application (Calculator)

CCP-503.10 26
Control Array application (Calculator)

CCP-503.10 27
Control Array application (Calculator)

CCP-503.10 28
Control Array application (Calculator)

CCP-503.10 29
Summary
 The Control Arrays in Visual Basic

CCP-503.10 30
Quiz
1. Control Arrays are added at run time
using _______
A) Load Object
B) Store Object
C) Run Object
D) UnLoad Object

CCP-503.10 31
2. Which property is NOT same for all the
Controls of Control Array ?

A) Caption
B) Name
C) Font
D) Index

CCP-503.10 32
Frequently Asked Questions

1. What is Control Array?

2. List any two advantages of control array.

3. Explain how to create Control Array at design time

4. Explain how to create Control Array at run time

CCP-503.10 33
Assignment
1. Create a Control array based application

CCP-503.10 34

Das könnte Ihnen auch gefallen