以编程方式向用户窗体添加命令按钮 [英] Programatically adding a commandbutton to a userform

查看:127
本文介绍了以编程方式向用户窗体添加命令按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 将ctrl = Me.Controls.Add添加到用户窗体中(_ 
bstrProgID:=Forms.CommandButton.1,_
名称:=CommandButton1,可见:= True)

现在我想知道如何点击它会怎么做?谢谢!!

解决方案

这是vba将允许的技术之一,但你大概不应该。由于所有相同的原因,您不应使用更​​改代码的代码。



说,这里是如何做你想要的。首先插入一个类模块并将其命名为DynBtn,然后将此代码粘贴到其中:

  Private WithEvents mobjBtn As MSForms.CommandButton 
私人msOnAction As String
''//这必须是通用的或通过名称调用将无法在您的表单中找到方法
''//。
私有的mobjParent作为对象

公共属性获取对象()作为MSForms.CommandButton
设置对象= mobjBtn
结束属性

公共功能Load(ByVal parentFormName As Object,ByVal btn As MSForms.CommandButton,ByVal procedure As String)As DynBtn
设置mobjParent = parentFormName
设置mobjBtn = btn
msOnAction = procedure
设置加载= Me
结束函数

私有子类Class_Terminate()
设置mobjParent =没有
设置mobjBtn =没有
结束Sub

Private Sub mobjBtn_Click()
CallByName mobjParent,msOnAction,VbMethod
End Sub

现在用你的形式使用这个,创建一个空白的用户表单并粘贴这个代码:

  Private Const mcsCmdBtn As String =Forms.CommandButton.1
私有mBtn()作为DynBtn

Private Sub UserForm_Initialize()
Dim i As Long
ReDim mBtn(1)As DynBtn
对于我= 0到UBound(mBtn)
设置mBtn(i)=新的DynBtn
下一个
''//一个线程
mBtn(0).Load(Me,Me.Controls .Add(mcsCmdBtn,Btn1,True),DoSomething)Object.Caption =Test 1
''//或使用块。
使用mBtn(1).Load(Me,Me.Controls.Add(mcsCmdBtn,Btn2,True),DoSomethingElse)Object
.Caption =Test 2
.Top = .Height + 10
End with
End Sub

Public Sub DoSomething()
MsgBoxIt worked!
End Sub

Public Sub DoSomethingElse()
MsgBoxYay!
End Sub

Private Sub UserForm_Terminate()
删除mBtn
End Sub


In excel vba I have added a commandbutton to userform... like below

      Set ctrl = Me.Controls.Add( _
      bstrProgID:="Forms.CommandButton.1", _
      Name:="CommandButton1", Visible:=True)

Now I wanted to know how would I tell it what to do when it is clicked? thanks!!

解决方案

This is one of those techniques that vba will let you do, but you probably shouldn't. For all the same reasons you shouldn't use code that alters your code.

That said, here is how to do what you want. First insert a class module and name it DynBtn, then paste this code into it:

Private WithEvents mobjBtn As MSForms.CommandButton
Private msOnAction As String
''// This has to be generic or call by name won't be able to find the methods
''// in your form.
Private mobjParent As Object

Public Property Get Object() As MSForms.CommandButton
    Set Object = mobjBtn
End Property

Public Function Load(ByVal parentFormName As Object, ByVal btn As MSForms.CommandButton, ByVal procedure As String) As DynBtn
    Set mobjParent = parentFormName
    Set mobjBtn = btn
    msOnAction = procedure
    Set Load = Me
End Function

Private Sub Class_Terminate()
    Set mobjParent = Nothing
    Set mobjBtn = Nothing
End Sub

Private Sub mobjBtn_Click()
    CallByName mobjParent, msOnAction, VbMethod
End Sub

Now to use this in your form, create a blank user form and paste this code into it:

Private Const mcsCmdBtn As String = "Forms.CommandButton.1"
Private mBtn() As DynBtn

Private Sub UserForm_Initialize()
    Dim i As Long
    ReDim mBtn(1) As DynBtn
    For i = 0 To UBound(mBtn)
        Set mBtn(i) = New DynBtn
    Next
    ''// One Liner
    mBtn(0).Load(Me, Me.Controls.Add(mcsCmdBtn, "Btn1", True), "DoSomething").Object.Caption = "Test 1"
    ''// Or using with block.
    With mBtn(1).Load(Me, Me.Controls.Add(mcsCmdBtn, "Btn2", True), "DoSomethingElse").Object
        .Caption = "Test 2"
        .Top = .Height + 10
    End With
End Sub

Public Sub DoSomething()
    MsgBox "It Worked!"
End Sub

Public Sub DoSomethingElse()
    MsgBox "Yay!"
End Sub

Private Sub UserForm_Terminate()
    Erase mBtn
End Sub

这篇关于以编程方式向用户窗体添加命令按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆