Skip to main content

Past Blast

Featured Products

Windows Mobile Developer Controls
Windows Mobile Developer Controls
Stay in touch using the DEVBUSS RSS feeds.
 

News

Windows Mobile Developer Controls
Sapphire Soltuions

Simulating control arrays in eVB

Written by Robert Levy  [author's bio]  [read 39087 times]
Edited by Derek

Download the code

Page 1  Page 2 

- In the "(Declarations)" section of the form's code define an array of controls. The upper bound - the number in the parenthesis - should be the same as the highest 'Index'.

  • Dim TextBoxes(3) As TextBox

- Set each element in the array to be a reference to a specific control on the form. It is important to use the "Set" statement - otherwise, eVB assumes you want the default property of the right-hand-side control rather than a reference to it.

  • Set TextBoxes(0) = Text0
  • Set TextBoxes(1) = Text1
  • Set TextBoxes(2) = Text2
  • Set TextBoxes(3) = Text3

- Now you can access the controls in a For Each...Next loop!

  • Dim control As TextBox
    For Each control In TextBoxes
    ...
    Next

Control arrays in VB6 also provide centralized event handling for all controls in the array. We can get that too:

- For each event that you need to handle in a centralized place, create a sub routine that accepts an index (as well as anything else needed for that event) as a parameter and performs the centralized event handling.

  • Private Sub TextBoxes_Change(index As Integer)
    ...
    End Sub

- Now this is the tedious part, each control in the array needs to pass its event handling over to this centralized function (using its index as a parameter).

  • Private Sub Text0_Change()
    TextBoxes_Change 0
    End Sub
  • Private Sub Text1_Change()
    TextBoxes_Change 1
    End Sub
  • Private Sub Text2_Change()
    TextBoxes_Change 2
    End Sub
  • Private Sub Text3_Change()
    TextBoxes_Change 3
    End Sub

And that's it - you have your eVB control array!

Listing 1: The Old Way

(credit to jdp162 for submitting this to the eVB Code Snippets section)

'To see how this example works make 3 frames on your form:
'Frame1, Frame2, Frame3 and a cmdbutton. Past this inside the cmdbutton.

Private Sub Command1_Click()
Dim x As Integer
For x = 1 To 3
CtrlA("Frame", x).Visible = False
Next x
End Sub

'Add this function to any form, this can replace a long select list.
Private Function CtrlA(cName As String, cIndex As Integer) As Controls
Dim i As Integer, CC As Integer
For i = 0 To Me.Controls.Count - 1
If Me.Controls(i).Name = cName & CStr(cIndex) Then
Set CtrlA = Me.Controls(i)
Exit Function
End If
Next i
End Function

Previous Page