vb6 - 如何找到控制数组中最大的元素索引?

发布于 2025-01-01 01:52:06 字数 284 浏览 0 评论 0原文

我正在动态加载和卸载表单上的一组命令按钮。

我可以这样做:

    Dim UnloadIndex As Integer
    For UnloadIndex = 1 To 20
        Unload frmMain.cmdAction(UnloadIndex)
    Next

但我并不总是有 20 个元素。有没有办法循环遍历每个直到到达末尾?

我知道我可以使用全局变量并跟踪该值,但我试图避免这种情况。

有什么建议请...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏末染殇 2025-01-08 01:52:06

使用 UBound() 返回指定维度的最高可用下标
一个数组。

Dim UnloadIndex As Integer 
For UnloadIndex = LBound(frmMain.cmdAction) To UBound(frmMain.cmdAction)
    Unload frmMain.cmdAction(UnloadIndex) 
Next 

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 
回忆那么伤 2025-01-08 01:52:06

如果它们不是连续的,您也可以这样做:

Dim Control as CommandButton
For Each Control in frmMain.cmdAction
  If Control.Index > 0 Then
    Unload Control
  End If
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
╄→承喏 2025-01-08 01:52:06
    Dim UnloadIndex As Integer 
For UnloadIndex = LBound(frmMain.cmdAction.LBound) To UBound(frmMain.cmdAction.UBound)
    Unload frmMain.cmdAction(UnloadIndex) 
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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文