What is a control array, how to used, and what is it for?

N

nosferatuss

I'm a Network Systems student,and i kind of dont understand the way my
teacher talks,I've try though,but I've only been speaking English for 2Years
and my teacher isn't from here;sois kind of hard. Any ways if you can give me
a hand on this, tht'd be terrific, it'll help me out a lot.
This Is my question:What is a control array, how to use it and what is it for?


Thank you.
 
J

Jezebel

First, you can't use them in VBA. They are specific to VB.

On a form you might have a set of controls of the same type -- such as
several textboxes. Instead of creating these as individual controls --
Text1, Text2, Text3, etc -- you can create an array of the same control --
Text1(0), Text1(1), Text1(2) ... This is done by setting by control's Index
property.

There are two main reasons for doing this:

1) You need to write the event code (eg for the click event) only once:

Private Sub Text1_Click(Index As Integer)

where Index tells you which particular textbox in the array was clicked.


2) You can add and remove additional members of the array at runtime.
Typically you create one member of the array at design time -- eg
Text1(0) -- then at runtime you can use Load Text1(1) to add an additional
array members.
 
H

Helmut Weber

Hi,
of course Jezebel is right, in principle,
though you could create an array of controls
in VBA the following way, even if only a workaraound:

Private Sub UserForm_Initialize()
' Count Optionbuttons
Dim oCnt As Control
Dim iCnt As Integer
For Each oCnt In Me.Controls
If TypeOf oCnt Is MSForms.OptionButton Then
iOpt = iOpt + 1
End If
Next
' create array of optionbuttons
ReDim ArrOpt(iOpt) As OptionButton
' assign each optionbutton
For Each oCnt In Me.Controls
If TypeOf oCnt Is MSForms.OptionButton Then
iCnt = iCnt + 1
Set ArrOpt(iCnt) = oCnt
' setting caption for testing
ArrOpt(iCnt).Caption = Format(iCnt, "Opt- 00")
End If
Next
End Sub

Note, that the "for each loop" over all controls,
seemably, processes them in the order they were created.

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top