请问怎样用代码动态生成按钮?
例如我要在一个窗口中动态生成30个Command按钮,纵向5个,横向6个
摆脱大家拉

解决方案 »

  1.   


       For i = 0 To 30
          Dim ctrl As Object
          Set ctrl = Me.Controls.Add("VB.CommandButton", "button" & CStr(i))
          ctrl.Visible = True
          ctrl.Top = 10 '自行设置Button的位置
          ctrl.Left = 10
          
       Next i
      

  2.   

    '这个也给我吧,哈哈
    Option ExplicitPrivate Sub Form_Load()
        Dim i As Integer
        Dim j As Integer
        For i = 1 To 5
            For j = 1 To 6
                Dim c As VB.CommandButton
                Set c = Me.Controls.Add("VB.CommandButton", "cmd" & i & j)
                c.Move i * 1000, j * 500, 800, 400
                c.Visible = True
            Next
        Next
    End Sub
      

  3.   

    Dim i,j As Integer
        Dim A,B As Integer
        
        A = 1200          '按需要调整 A,B
        B = 800
        Dim cmd(29) As VB.CommandButton
        For i = 0 To 5
            For j = 0 To 4
                Set cmd(i * 5 + j) = Me.Controls.Add("VB.CommandButton", "cmd" & i & j)
                cmd(i * 5 + j).Move j * A + 1000, i * B + 500, 800, 400
                cmd(i * 5 + j).Visible = True
                cmd(i * 5 + j).Caption = "Command" & (i * 5 + j)
            Next j
        Next i