【问题标题】:Working with dynamic event handler in vba在 vba 中使用动态事件处理程序
【发布时间】:2013-11-21 22:36:51
【问题描述】:

我创建了一个表单,我在其中动态创建文本框和相应的组合框以及组合框更改事件。这是创建组合框事件处理程序的类

Option Explicit

Public WithEvents cbx As MSforms.Combobox
Private avarSplit As Variant

Sub SetCombobox(ctl As MSforms.Combobox)
  Set cbx = ctl
End Sub

Private Sub cbx_Change()
 Dim i As Integer
  If cbx.ListIndex > -1 Then
  'MsgBox "You clicked on " & cbx.Name & vbLf & "The value is " & cbx.Value
  avarSplit = Split(cbx.Name, "_")
    'DecessionOnValue
 End If
End Sub

这是动态创建文本框和组合框的表单上的代码

Function AddTextBox(Frame1 As frame, numberOfColumns As Integer)

 Dim counter As Integer
 Dim i As Integer
 Dim TxtBox As MSforms.TextBox
 For counter = 1 To numberOfColumns
                                     'Forms.CommandButton.1
    Set TxtBox = Frame1.Controls.Add("Forms.TextBox.1")
    TxtBox.Name = "tb_" + CStr(counter)
    'Bouton.Caption = "Test"
    TxtBox.Visible = True
    i = Property.TextBoxDisable(TxtBox)
    ' Defining coordinates  TextBox height is 18
    If counter = 1 Then
        TxtBox.Top = 23
    Else
        TxtBox.Top = (18 * counter) + 5 * counter
    End If
        TxtBox.Left = 50
   Next counter
End Function

Function Combobox(Frame1 As frame, numberOfColumns As Integer)

Dim counter As Integer
Dim i As Integer
Dim CbBox As MSforms.Combobox
Dim cbx As ComboWithEvent

If pComboboxes Is Nothing Then Set pComboboxes = New Collection
   For counter = 1 To numberOfColumns
                                     'Forms.CommandButton.1
    Set CbBox = Frame1.Controls.Add("Forms.ComboBox.1")
    CbBox.Name = "cb_" + CStr(counter)
    i = AddComboboxValues(CbBox)
  ' Defining coordinates  TextBox height is 18
    If counter = 1 Then
        CbBox.Top = 23
    Else
        CbBox.Top = (18 * counter) + 5 * counter
    End If
        CbBox.Left = 150
        Set cbx = New ComboWithEvent
        cbx.SetCombobox CbBox
        pComboboxes.Add cbx
    Next counter
    i = AddScrollBar(Frame1, counter)

End Function

组合框事件处理程序工作正常,但我的问题是我不知道如何根据动态组合框中选择的值复制文本框的文本或启用禁用文本框。

谢谢, 贾廷

【问题讨论】:

  • 您需要在每个组合框中动态插入事件处理代码。

标签: excel vba


【解决方案1】:

你会用这个来举例:

Me.Controls("ControlName").Visible = True 或者你可以使用启用、禁用等代替 Visible。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    相关资源
    最近更新 更多