2013-02-11 80 views
0

我創建了一個表單,在該表單中我動態創建了文本框和相應的組合框以及組合框更改事件。這裏是類創建組合框事件處理在vba中使用動態事件處理程序

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 

組合框事件處理程序工作正常窗體上的代碼,但我的問題是,我不知道如何複製文本框的文本或根據動態組合框中選擇的值啓用禁用文本框。

感謝, Jatin

+0

您需要動態地將事件處理代碼插入到每個組合框中。 – Pynner 2013-02-11 13:28:18

回答

0

你會用這個例子:

Me.Controls( 「控件名稱」)可見=真或代替可見,您可以使用啓用,禁用等。

相關問題