用循环简化VB代码

来源:百度知道 编辑:UC知道 时间:2024/06/04 14:49:41
Static MaxIdx
For i = 1 To Val(a)
Command1(0).Width = zjm.Width / (a / 7)
Command1(0).Height = zjm.Height / (a / 6)
MaxIdx = MaxIdx + i
Load Command1(MaxIdx)
Command1(MaxIdx).Visible = True
Command1(MaxIdx).Left = Command1(0).Width
Command1(MaxIdx).top = Command1(0).Height * (i - 1) + 2000
If i > 8 Then
Command1(MaxIdx).Left = Command1(0).Width * 2
Command1(MaxIdx).top = Command1(0).Height * (i - 9) + 2000
End If
If i > 16 Then
Command1(MaxIdx).Left = Command1(0).Width * 3
Command1(MaxIdx).top = Command1(0).Height * (i - 17) + 2000
End If
If i > 24 Then
Command1(MaxIdx).Left = Command1(0).Width * 4
Command1(MaxIdx).top = Command1(0).Height * (i - 25) + 2000
End If
If i > 32 Then
Command1(MaxIdx).Left = Command1(0).Width * 5
Command1(MaxIdx).top = Command1(0).Height * (i - 33) + 2000
End If
If i > 40 Then
Command1(MaxIdx).Left = Command1(0).Wid

Static MaxIdx
For i = 1 To Val(a)
    Command1(0).Width = zjm.Width / (a / 7)
    Command1(0).Height = zjm.Height / (a / 6)

''''''''''''''''''''''''''''''''''''''''
''简化后代码
''真是膜拜楼主了,3句话搞定的代码经验写那么多,要是有1000个按钮粘贴复制也要累趴下了...
''另外不知道你这个MaxIdx是干嘛的,所以给你保留了,如果只是数组的下标,建议直接用i好了
''''''''''''''''''''''''''''''''''''''''
    MaxIdx = MaxIdx + i
    '加载按钮
    Load Command1(MaxIdx)

    '每列8个,调整新按钮位置
    Command1(MaxIdx).Left = Command1(0)