使用VBA将控件添加到Excel用户窗体中的帧

时间:2009-02-19 04:39:39

标签: excel vba excel-vba userform

我需要动态创建标签和按钮,然后将它们添加到用户窗体内的框架中。我该怎么做呢?似乎它应该比实际更容易。

3 个答案:

答案 0 :(得分:18)

以下代码演示了如何使用控件动态填充用户窗体中的帧...

在我使用的表单中,我有一个名为Frame1的帧控件,因此在UserForm_Initialize中,您调用Frame1.Controls.Add以在帧中嵌入控件。您可以设置控件,该控件返回到您在UserForm代码模块中定义的WithEvents控件变量,以便您可以响应您想要的任何控件上的事件...

因此,使用此方法,您需要为您创建的任何控件预先编写所需的任何事件代码...

另请注意,即使顶部,左侧,宽度和高度属性不一定出现在intellisense中,您也可以对控件进行定位和调整大小...

Private WithEvents Cmd As MSForms.CommandButton
Private WithEvents Lbl As MSForms.Label

Private Sub UserForm_Initialize()
    Set Lbl = Frame1.Controls.Add("Forms.Label.1", "lbl1")
    Lbl.Caption = "Foo"
    Set Cmd = Frame1.Controls.Add("Forms.CommandButton.1", "cmd1")
End Sub

Private Sub Cmd_Click()
    Cmd.Top = Cmd.Top + 5
End Sub

Private Sub Lbl_Click()
    Lbl.Top = Lbl.Top + 5
End Sub

答案 1 :(得分:4)

我对上述主题的变化。这仅适用于4x4按钮阵列。创建用户表单并将其添加到其代码中。您的标签可以使用相同的概念(或参见上一个答案):

Private cmdLots(20) As MSForms.CommandButton

Private Sub UserForm_Initialize()
For i = 1 To 4
For j = 1 To 4
    k = i + (4 * j)
    Set cmdLots(k) = UserForm2.Controls.Add("Forms.CommandButton.1", "cmd1")
    With cmdLots(k)
        .Top = i * 25
        .Left = (j * 80) - 50
        .BackColor = RGB(50 * i, 50 * j, 0)
        .Caption = "i= " & i & "  j= " & j
    End With
Next j
Next i
End Sub

答案 2 :(得分:0)

Add方法

要将控件添加到用户窗体或框架中,请使用add方法。

SetControl = object.Add(ProgID [, Name [, Visible ]] )

第一个参数将引用您要添加的控件类型,它是ProgID,定义为

程序标识符。没有空格的文本字符串,用于标识对象类。 ProgID的标准语法是... ProgID映射到类标识符(CLSID)。

功能解决方案

为简化此过程,我们使用和枚举来帮助管理各种控件。

' List of all the MSForms Controls.
Public Enum MSFormControls
    CheckBox
    ComboBox
    CommandButton
    Frame
    Image
    Label
    ListBox
    MultiPage
    OptionButton
    ScrollBar
    SpinButton
    TabStrip
    TextBox
    ToggleButton
End Enum

有了这个枚举,我们现在可以轻松地创建一个函数来获取ProgID字符串。

' Gets the ProgID for each individual control. Used to create controls using `Object.add` method.
' @see https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/add-method-microsoft-forms
Public Function GetMSFormsProgID(control As MSFormControls) As String
    Select Case control
      Case MSFormControls.CheckBox:       GetMSFormsProgID = "Forms.CheckBox.1"
      Case MSFormControls.ComboBox:       GetMSFormsProgID = "Forms.ComboBox.1"
      Case MSFormControls.CommandButton:  GetMSFormsProgID = "Forms.CommandButton.1"
      Case MSFormControls.Frame:          GetMSFormsProgID = "Forms.Frame.1"
      Case MSFormControls.Image:          GetMSFormsProgID = "Forms.Image.1"
      Case MSFormControls.Label:          GetMSFormsProgID = "Forms.Label.1"
      Case MSFormControls.ListBox:        GetMSFormsProgID = "Forms.ListBox.1"
      Case MSFormControls.MultiPage:      GetMSFormsProgID = "Forms.MultiPage.1"
      Case MSFormControls.OptionButton:   GetMSFormsProgID = "Forms.OptionButton.1"
      Case MSFormControls.ScrollBar:      GetMSFormsProgID = "Forms.ScrollBar.1"
      Case MSFormControls.SpinButton:     GetMSFormsProgID = "Forms.SpinButton.1"
      Case MSFormControls.TabStrip:       GetMSFormsProgID = "Forms.TabStrip.1"
      Case MSFormControls.TextBox:        GetMSFormsProgID = "Forms.TextBox.1"
      Case MSFormControls.ToggleButton:   GetMSFormsProgID = "Forms.ToggleButton.1"
    End Select
End Function

最后,让我们创建一个使用新功能添加到表单或框架中的功能。

' Easly add control to userform or a frame.
' @returns {MSForms.control} The control that was created
Public Function AddControl(userformOrFrame As Object _
                         , control As MSFormControls _
                         , Optional name As String = vbNullString _
                         , Optional visable As Boolean = True _
                        ) As MSForms.control
    Set AddControl = userformOrFrame.Controls.Add(GetMSFormsProgID(control), name, visable)
End Function

演示

要演示它,我们可以通过循环枚举将每个控件添加到空白用户窗体中。

Private Sub UserForm_Initialize()
    demoAddingControlsToUserform
End Sub

Private Sub demoAddingControlsToUserform()
    ' Offset used to prevent controls
    ' overlapping as well as provide
    ' a height for the scrollbars
    Dim offsetHeight As Double
    
    ' Add each control to the userform
    ' and set top to make sure they are not overlapping
    Dim index As MSFormControls
    For index = CheckBox To ToggleButton
        With AddControl(Me, index)
            .Top = offsetHeight
            offsetHeight = offsetHeight + .Height
        End With
    Next
    
    ' Show scrollbars and adjust the height to show
    ' all the added controls.
    With Me
        .ScrollBars = fmScrollBarsVertical
        .ScrollHeight = offsetHeight + 20
    End With
End Sub