Results 1 to 2 of 2

Thread: Converting a collection into a control array

  1. #1

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    Converting a collection into a control array

    Here is a simple example of converting from a collection of controls into a control array.
    Personally I much prefer working with control arrays than using control enumeration, so thats why I wrote this.

    This example does however use control enumeration to find all the controls in the first place.

    Create a form, and place 6 command buttons on it.
    If prompted, do not use a control array.

    VB Code:
    1. Private Function createControlArray(ByRef controlCollection As Object) As Object()
    2.     If Not controlCollection.Count = 0 Then
    3.         Dim retVal() As Object: ReDim retVal(controlCollection.Count)
    4.         Dim i As Long: For i = 0 To UBound(retVal) - 1: Set retVal(i) = controlCollection.Item(i): Next
    5.         createControlArray = retVal
    6.     End If
    7. End Function
    8.  
    9. Private Sub Form_Load()
    10.     Command1.Caption = "Command1": Command2.Caption = "Command2": Command3.Caption = "Command3":
    11.     Command4.Caption = "Command4": Command5.Caption = "Command5": Command6.Caption = "Command6":
    12.     Dim cmdButtons() As Object: cmdButtons = createControlArray(Form1.Controls)
    13.     MsgBox "Caption of third command button : " & cmdButtons(3).Caption
    14. End Sub
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  2. #2

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    * 21-October-2004 - Moved to CodeBank *
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width