下面代码产生含有9个按钮的数组控件,Unload Command1(2)之后,后面的Command(3),Command(4),Command(5)...的下标,有没有办法依次前移,变成Command(2),Command(3),Command(4)...
Private Sub Form_Load()
    For i = 1 To 9
        Load Command1(i)
    Next
End Sub

解决方案 »

  1.   

    '加入到集合ComButton,Unload一个后,其余的到集合中去找
    Private Sub Form_Load()
        set ComButton as As New Collection
        For i = 1 To 9
            Load Command1(i)
            ComButton.add Command1(i)  
        Next
    End Sub
    '到集合中找
    Dim C As Control 
    For Each Obj In ComButton
        debug.print C.Name
    next
      

  2.   

    '到集合中找
    Dim C As Control 
    For Each C In ComButton
        debug.print C.Name
    next
      

  3.   

    你试一下,在Unload Command1(2)之后,执行:For i=2 to 8
        Set Command1(i) = Command1(i+1)
    Next
      

  4.   

    甚至不用unload,只要隐藏起来就可以。
      

  5.   


    还是会出现控件数组元素2不存在的错误,即使是用了ComButton.Remove之后.
    不管是用 For Each C In ComButton 还是For i = 0 to ComButton.Count - 1
      

  6.   

    不知楼主的 Command1() 是如何定义的?我在窗体模块中定义:
    Dim objCmdBut(1 To 16) As CommandButton执行:Load objCmdBut(i) 出现实时错误
    实时错误91: 对象变量或 With 块变量未设置
    我在窗体模块中定义:
    Dim objCmdBut(1 To 16) As New CommandButton编译错误: 无效使用 New 关键字
    我的程序中只有用 Controls.Add 方法动态创建控件。我用的是VB6,请问楼主用的是啥环境?
      

  7.   

    VB6
    先手工创建1个控件,将其Index设为0,然后就可以通过Load Command1(i)来动态创建其他的
      

  8.   

    '例如你点击 2 则把 2 3 4 5 都干掉,再从2开始加载看你要几个Dim i&, Tcom&
    Private Sub Form_Load()
       Me.Width = 8000
       Command1(0).Move 0, 0
       Command1(0).Caption = "0"
       For i = 1 To 5
          Load Command1(i)
          Command1(i).Caption = CStr(i)
          Command1(i).Visible = True
          Command1(i).Move Command1(i - 1).Left + Command1(0).Width
       Next i
       Tcom = Command1.Count
    End SubPrivate Sub Command1_Click(Index As Integer)
       If Index <> 0 Then
          For i = Index To Command1.Count - 1
             Unload Command1(i)
          Next i
          MsgBox "现在你只剩下 " & CStr(Index) & " 个Command1"
          For i = Index To Tcom - 1
             Load Command1(i)
             Command1(i).Caption = CStr(i)
             Command1(i).Visible = True
             Command1(i).Move Command1(i - 1).Left + Command1(0).Width
          Next i
       End If
    End Sub
      

  9.   

    Dim i&, Tcom&, Fsize&
    Private Sub Form_Load()
       Me.Width = 8000
       Command1(0).Move 0, 0
       Command1(0).Caption = "0"
       Fsize = 10
       For i = 1 To 5
          Load Command1(i)
          Command1(i).Caption = Fsize
          Command1(i).FontSize = Fsize
          Command1(i).Visible = True
          Command1(i).Move Command1(i - 1).Left + Command1(0).Width
          Fsize = Fsize + 3
       Next i
       Tcom = Command1.Count
    End SubPrivate Sub Command1_Click(Index As Integer)
       If Index <> 0 Then
          If Index <> Tcom - 1 Then
             For i = Index To Tcom - 1 - 1
                Command1(i).Caption = Command1(i + 1).Caption
                Command1(i).FontSize = Command1(i + 1).FontSize
                '其它的属性一一带过来
             Next i
             Unload Command1(Tcom - 1)
             Tcom = Command1.Count
          End If
      End If
    End SubPrivate Sub Command1_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
       Me.Caption = Index
    End Sub
      

  10.   

    存在控件里内容不一定就是Caption等属性,如果是WebBrowser控件,就有可能是打开的WEB页面,如果转内容的话,就需要重新向服务器请求内容的.
      

  11.   

    加一句:on error resume next
    找不到,就找下一个就对了