Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
nosferatuss - 19 Feb 2005 02:05 GMT
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.
Jezebel - 19 Feb 2005 03:06 GMT
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.

> 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
[quoted text clipped - 6 lines]
>
> Thank you.
Helmut Weber - 20 Feb 2005 01:11 GMT
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/
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.