为什么下面的语句不行:
Dim but1 As CommandButton
Set but1 = CreateObject("CommandButton")
With but1
.Top = 2000
.Left = 10000
.Height = 300
.Caption = "ok"
End With

解决方案 »

  1.   

    你只是定义了按钮,没有加载。
    Dim but1 As CommandButton
    Set but1 = CreateObject("CommandButton")
    Load but1
    With but1
    .Top = 2000
    .Left = 10000
    .Height = 300
    .Caption = "ok"
    End With
      

  2.   

    Private Sub Form_Load()
        Form1.Controls.Add "vb.commandbutton", "cmdok"   'dynamical load the button
        Dim ctl
        With Form1!cmdok
            .Visible = True
            .Width = 4500
            .Caption = "hello"
            .Left = Me.Width / 2
            .Top = Me.Height / 2
        End With
        Set ctl = Form1!cmdok
    End Sub
      

  3.   

    不用加载吧?以下应该算是最简单的动态生成按钮的方法了!
    Private WithEvents cmdMyButton As CommandButtonPrivate Sub Form_Load()
      Set cmdButton=Controls.Add("VB.CommandButton","Button")
      With cmdButton
      .visible=True
      .Width=3000
      .Capation="This is a button"
      .Top=1000
      .Left=1000
      End With
    End sub
      

  4.   

    关键你没把它的 visible属性改成true
      

  5.   

    Dim but1 As CommandButton
    Set but1 = CreateObject("CommandButton") ''问题是就在这一句就报错,说“ActiveX 部件 不能创建对象或返回对该对象的引用”,这是为什么?
    With but1
    .Top = 2000
    .Left = 10000
    .Height = 300
    .Caption = "ok"
    End With
    我的意思是如果我用createobject该怎样做才不会报错。是不是progid错误,括号里不应是“CommandButton”?
      

  6.   

    createobject的功能是创建一个()类型的ActiveX对象的引用,而参数中似乎并没有commandbutton类