vb6 - how to find largest element index in control array?
-
21-04-2021 - |
Question
I am dynamically loading and unloading an array of command buttons on a form.
I can do this:
Dim UnloadIndex As Integer
For UnloadIndex = 1 To 20
Unload frmMain.cmdAction(UnloadIndex)
Next
But I don't always have 20 elements. Is there a way to loop through each one until it reaches the end?
I know I can use a global variable and track the value but I'm trying to avoid this.
Any suggestions please...
Solution
Use UBound()
which returns the highest available subscript for the indicated dimension of
an array.
Dim UnloadIndex As Integer
For UnloadIndex = LBound(frmMain.cmdAction) To UBound(frmMain.cmdAction)
Unload frmMain.cmdAction(UnloadIndex)
Next
OTHER TIPS
If they're not sequential, you could also do:
Dim Control as CommandButton
For Each Control in frmMain.cmdAction
If Control.Index > 0 Then
Unload Control
End If
Next
Dim UnloadIndex As Integer
For UnloadIndex = LBound(frmMain.cmdAction.LBound) To UBound(frmMain.cmdAction.UBound)
Unload frmMain.cmdAction(UnloadIndex)
Next
I found that the accepted answer way gives a compile error
expected array
Using dot notation instead worked for me.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow