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...

有帮助吗?

解决方案

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 

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top