You are on page 1of 3

Control Arrays in Visual Basic 6

A control array is a group of controls that share the same name type and the same event
procedures. Adding controls with control arrays uses fewer resources than adding multiple
control of same type at design time.

A control array can be created only at design time, and at the very minimum at least one control
must belong to it. You create a control array following one of these three methods:

 You create a control and then assign a numeric, non-negative value to its Index property;
you have thus created a control array with just one element.
 You create two controls of the same class and assign them an identical Name property.
Visual Basic shows a dialog box warning you that there's already a control with that
name and asks whether you want to create a control array. Click on the Yes button.
 You select a control on the form, press Ctrl + C to copy it to the clipboard, and then press
Ctrl + V to paste a new instance of the control, which has the same Name property as the
original one. Visual Basic shows the warning mentioned in the previous bullet.

Control arrays are one of the most interesting features of the Visual Basic environment, and they
add a lot of flexibility to your programs:

 Controls that belong to the same control array share the same set of event procedures; this
often dramatically reduces the amount of code you have to write to respond to a user's
actions.
 You can dynamically add new elements to a control array at run time; in other words, you
can effectively create new controls that didn't exist at design time.
 Elements of control arrays consume fewer resources than regular controls and tend to
produce smaller executables. Besides, Visual Basic forms can host up to 256 different
control names, but a control array counts as one against this number. In other words,
control arrays let you effectively overcome this limit.

MessageBox Function in Visual Basic 6 (VB6)


Displays a message in a dialog box and wait for the user to click a button, and returns an integer
indicating which button the user clicked.

Following is an expanded MessageBox


Syntax :

MsgBox ( Prompt [,icons+buttons ] [,title ] )

memory_variable = MsgBox ( prompt [, icons+ buttons] [,title] )

Prompt : String expressions displayed as the message in the dialog box. If prompt consist of
more than one line, you can separate the lines using the vbrCrLf constant.

Icons + Buttons : Numeric expression that is the sum of values specifying the number and type
of buttons and icon to display.

Title : String expression displayed in the title bar of the dialog box. If you omit title, the
application name is placed in the title bar.

User-Defined Data Types in Visual Basic 6


Variables of different data types when combined as a single variable to hold several related
informations is called a User-Defined data type.

A Type statement is used to define a user-defined type in the General declaration section of a
form or module. User-defined data types can only be private in form while in standard modules
can be public or private. An example for a user defined data type to hold the product details is as
given below.

Private Type ProductDetails


ProdID as String
ProdName as String
Price as Currency
End Type

The user defined data type can be declared with a variable using the Dim statement as in any
other variable declaration statement. An array of these user-defined data types can also be
declared. An example to consolidate these two features is given below.
Dim ElectronicGoods as ProductDetails ' One Record
Dim ElectronicGoods(10) as ProductDetails ' An array of 11 records

A User-Defined data type can be referenced in an application by using the variable name in the
procedure along with the item name in the Type block. Say, for example if the text property of a
TextBox namely text1 is to be assigned the name of the electronic good, the statement can be
written as given below.

Text1.Text = ElectronicGoods.ProdName

If the same is implemented as an array, then the statement becomes

Text1.Text = ElectronicGoods(i).ProdName

User-defined data types can also be passed to procedures to allow many related items as one
argument.

Sub ProdData( ElectronicGoods as ProductDetails)


Text1.Text = ElectronicGoods.ProdName
Text1.Text = ElectronicGoods.Price
End Sub

You might also like