HOWTO: VB3: Pass an Array of Controls to a SUB or FUNCTION

ID: Q143426

The information in this article applies to:

- Microsoft Visual Basic programming system for Windows, version 3.0

SUMMARY

In Microsoft Visual Basic version 3.0 for Windows, it is not possible to pass a control array to a user defined SUB or FUNCTION. You can only pass elements of a control array.

WORKAROUND

In order to work around this limitation, you can create an array of specific objects and pass this array to a Sub or Function.

Step-by-Step example

The following example converts the content of all the Textboxes on a form to upper-case characters when the user clicks on the Command Button.

1. Start Visual Basic for Windows. If Visual Basic is already running,

   click New Project on the File menu (ALT+F+N). Form1 is created by
   default.

2. Add a Command Button named Command1 to the form.

3. Add 3 text boxes to the form named Text1.

4. Add the following code to the general declarations section of Form1:

      Sub AllUcase(txt() As TextBox)
        Dim i As Integer
        For i = LBound(txt) To UBound(txt)
            txt(i) = UCase(txt(i))
        Next i
      End Sub

5. In the Command1_Click event of Form1, add the following code:

      Sub Command1_Click()
        On Error Resume Next
        Dim i As Integer
        Dim Again As Integer
        ReDim Tabtxt(0) As TextBox

        i = 0
        Again = True
        While Again
          Text1(i).Tag = Text1(i).Tag
          If Err = 0 Then
            Redim Preserve TabTxt(0 To i)
            Set TabTxt(i) = Text1(i)
          Else
            Again = False
          End If
          i = i + 1
        Wend
        Call AllUcase(TabTxt())
      End Sub

6. Press ALT+F+V to save the project. Then press F5 to run the program.
   Click once on the form and exit the application.

7. Once the program is running, enter values in the different text boxes
   and click on the Command1 button.

In Visual Basic 4.0, a control array can be passed as an Object. The following code passes a control array of TextBoxes, and adds another TextBox to the control array. The call to the SUB would be as follows:

      AddControlArrayElement Text1

The code in the procedure would be as follows:

   Sub AddControlArrayElement (ControlArray as Object)
     NextElement% = ControlArray.Count
     Load ControlArray(NextElement%)
     ControlArray(NextElement%).Top = ControlArray(NextElement%-1).Top+100
     ControlArray(NextElement%).Visible=True
   End Sub
Keywords          : vbwin 
Version           : 3.0
Platform          : WINDOWS

Last Reviewed: March 25, 1997