下面代码产生含有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
Private Sub Form_Load()
For i = 1 To 9
Load Command1(i)
Next
End Sub
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
Dim C As Control
For Each C In ComButton
debug.print C.Name
next
Set Command1(i) = Command1(i+1)
Next
还是会出现控件数组元素2不存在的错误,即使是用了ComButton.Remove之后.
不管是用 For Each C In ComButton 还是For i = 0 to ComButton.Count - 1
Dim objCmdBut(1 To 16) As CommandButton执行:Load objCmdBut(i) 出现实时错误
实时错误91: 对象变量或 With 块变量未设置
我在窗体模块中定义:
Dim objCmdBut(1 To 16) As New CommandButton编译错误: 无效使用 New 关键字
我的程序中只有用 Controls.Add 方法动态创建控件。我用的是VB6,请问楼主用的是啥环境?
先手工创建1个控件,将其Index设为0,然后就可以通过Load Command1(i)来动态创建其他的
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
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
找不到,就找下一个就对了