English 中文(简体)
使用VBA在Excel用户窗体中向框架添加控件
原标题:
  • 时间:2009-02-19 04:39:39
  •  标签:

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

最佳回答

以下代码演示了如何动态地将控件填充到用户窗体中的框架中...

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

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

请注意,即使在智能感知中没有显示顶部、左侧、宽度和高度属性,您仍然可以定位和调整控件的大小。

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
问题回答

我对以上主题的变化。这仅适用于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

The Add Method

要向用户窗体或框架添加控件,可以使用 add 方法。

SetControl = object.Add(ProgID [, 名称 [, 可见性]])

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

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

A Functional Solution

为了使这个过程更容易,让我们使用枚举来帮助管理各种控件。

  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://learn.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

使用此类枚举的美妙之处在于,我们现在可以为所有控件提供智能感知,而无需记忆它们全部。

将此翻译成中文:演示智能感知

Demo

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

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
      (Although this looks odd, you can actually loop enums this way.)
    Dim control As MSFormControls
    For control = CheckBox To ToggleButton
        With AddControl(Me, control)
            .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

请将此翻译成中文:Demo Userform with all controls 演示用户表单和所有控件。





相关问题
热门标签