vb6: ¿cómo encontrar el índice de elemento más grande en la matriz de control?

I am dynamically loading and unloading an array of command buttons on a form.

Puedo hacer esto:

    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.

Cualquier sugerencia por favor ...

preguntado el 01 de febrero de 12 a las 04:02

3 Respuestas

Utilizan 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 

Respondido 01 Feb 12, 08:02

Thank you for that. I also needed to keep the very first button. So I decided to go from 1 to UBound... Thanks and +1 for your quick response. - itsols

+1 although you have linked to the VB.Net docs. Here is the VB6 topic for Ubound msdn.microsoft.com/en-us/library/aa263396(v=vs.60).aspx - MarkJ

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

Respondido 01 Feb 12, 13:02

+1 for drawing attention to the often over-looked fact that there can be gaps. Also possible to start at an arbitrary lower bound. - tcarvin

    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.

respondido 03 mar '17, 11:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.