更改对列表框的引用以添加项目
Changing reference to Listbox to add item
我有一个用户窗体,里面有多个列表框和一个用于添加项目的按钮。
it looks like this.
当用户单击一个按钮时,他可以按名称 select 列表框并添加新事件(它现在是静态类型的)。当用户尝试保存它时,代码将调用该函数,该函数应更改对列表框的引用,应在其中添加和创建新项目。
我看到这样的草稿,但我不知道如何处理这个列表框名称(见评论):
Function AddItemToListbox(listBoxName as String)
Select case listBoxName
case listBoxName1
'here I'd like to set listbox
'something like set listbox=listBox1
case listBoxName2
'here I'd like to set listbox
case listBoxName3
'here I'd like to set listbox
case listBoxName4
'here I'd like to set listbox
End Select
listbox.addItem
'and do the other stuff with this listbox which I need to do
End Function
你有解决办法或参考资料可以解释吗?
就我个人而言,这就是我使用两种形式处理它的方式
我的主窗体叫做 UserForm1
,我的 'Event Picker' 叫做 EventPicker
UserForm1
已设置为您的示例
和EventPicker
显示
在我的UserForm1
背后有以下代码
Option Explicit
Private Sub CommandButton1_Click()
Dim ctrl
Dim Eventfrm As EventPicker
Set Eventfrm = New EventPicker
Set Eventfrm.ParentForm = Me
For Each ctrl In Me.Controls
If TypeName(ctrl) = "ListBox" Then
Eventfrm.NamePickerCombo.AddItem ctrl.Name
End If
Next ctrl
Eventfrm.Show
End Sub
这在 'Add Event' 按钮的 Click
上调用,并通过填充 ComboBox
和设置 ParentForm
[=63] 来启动 EventPicker
表单=] 以便子窗体知道它来自哪里。
在我的 EventPicker
表单后面有以下代码
Option Explicit
Private vParentForm As UserForm1
Public Property Set ParentForm(v As UserForm1)
Set vParentForm = v
End Property
Public Property Get ParentForm() As UserForm1
Set ParentForm = vParentForm
End Property
Private Sub CommandButton1_Click()
Dim lBox As MSForms.ListBox
Set lBox = ParentForm.Controls(Me.NamePickerCombo.Value)
lBox.AddItem Me.TextBox1.Value
Unload Me
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
留给我以下内容
使用名称代替控件的更新Name
s。
落后于UserForm1
Option Explicit
Private Sub CommandButton1_Click()
Dim ctrl
Dim Eventfrm As EventPicker
Set Eventfrm = New EventPicker
Set Eventfrm.ParentForm = Me
Debug.Print Me.Name, TypeName(Me)
For Each ctrl In Me.Controls
Debug.Print ctrl.Name, TypeName(ctrl)
If TypeName(ctrl) = "Label" Then
Eventfrm.NamePickerCombo.AddItem ctrl.Caption
End If
Next ctrl
Eventfrm.Show
End Sub
落后于EventPicker
Option Explicit
Private vParentForm As UserForm1
Public Property Set ParentForm(v As UserForm1)
Set vParentForm = v
End Property
Public Property Get ParentForm() As UserForm1
Set ParentForm = vParentForm
End Property
Private Sub CommandButton1_Click()
Dim lBox As MSForms.ListBox
Dim lbl As MSForms.Label
Dim ctrl As Control
Debug.Print Me.NamePickerCombo.Value, Me.TextBox1.Value
For Each ctrl In ParentForm.Controls
If TypeName(ctrl) = "Label" Then
If ctrl.Caption = Me.NamePickerCombo.Value Then
Set lbl = ctrl
Exit For
End If
End If
Next ctrl
For Each ctrl In ParentForm.Controls
If ctrl.Left >= lbl.Left And ctrl.Left + ctrl.Width <= lbl.Left + lbl.Width Then
Set lBox = ctrl
Exit For
End If
Next ctrl
lBox.AddItem Me.TextBox1.Value
Unload Me
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
我有一个用户窗体,里面有多个列表框和一个用于添加项目的按钮。 it looks like this. 当用户单击一个按钮时,他可以按名称 select 列表框并添加新事件(它现在是静态类型的)。当用户尝试保存它时,代码将调用该函数,该函数应更改对列表框的引用,应在其中添加和创建新项目。
我看到这样的草稿,但我不知道如何处理这个列表框名称(见评论):
Function AddItemToListbox(listBoxName as String)
Select case listBoxName
case listBoxName1
'here I'd like to set listbox
'something like set listbox=listBox1
case listBoxName2
'here I'd like to set listbox
case listBoxName3
'here I'd like to set listbox
case listBoxName4
'here I'd like to set listbox
End Select
listbox.addItem
'and do the other stuff with this listbox which I need to do
End Function
你有解决办法或参考资料可以解释吗?
就我个人而言,这就是我使用两种形式处理它的方式
我的主窗体叫做 UserForm1
,我的 'Event Picker' 叫做 EventPicker
UserForm1
已设置为您的示例
和EventPicker
显示
在我的UserForm1
背后有以下代码
Option Explicit
Private Sub CommandButton1_Click()
Dim ctrl
Dim Eventfrm As EventPicker
Set Eventfrm = New EventPicker
Set Eventfrm.ParentForm = Me
For Each ctrl In Me.Controls
If TypeName(ctrl) = "ListBox" Then
Eventfrm.NamePickerCombo.AddItem ctrl.Name
End If
Next ctrl
Eventfrm.Show
End Sub
这在 'Add Event' 按钮的 Click
上调用,并通过填充 ComboBox
和设置 ParentForm
[=63] 来启动 EventPicker
表单=] 以便子窗体知道它来自哪里。
在我的 EventPicker
表单后面有以下代码
Option Explicit
Private vParentForm As UserForm1
Public Property Set ParentForm(v As UserForm1)
Set vParentForm = v
End Property
Public Property Get ParentForm() As UserForm1
Set ParentForm = vParentForm
End Property
Private Sub CommandButton1_Click()
Dim lBox As MSForms.ListBox
Set lBox = ParentForm.Controls(Me.NamePickerCombo.Value)
lBox.AddItem Me.TextBox1.Value
Unload Me
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
留给我以下内容
使用名称代替控件的更新Name
s。
落后于UserForm1
Option Explicit
Private Sub CommandButton1_Click()
Dim ctrl
Dim Eventfrm As EventPicker
Set Eventfrm = New EventPicker
Set Eventfrm.ParentForm = Me
Debug.Print Me.Name, TypeName(Me)
For Each ctrl In Me.Controls
Debug.Print ctrl.Name, TypeName(ctrl)
If TypeName(ctrl) = "Label" Then
Eventfrm.NamePickerCombo.AddItem ctrl.Caption
End If
Next ctrl
Eventfrm.Show
End Sub
落后于EventPicker
Option Explicit
Private vParentForm As UserForm1
Public Property Set ParentForm(v As UserForm1)
Set vParentForm = v
End Property
Public Property Get ParentForm() As UserForm1
Set ParentForm = vParentForm
End Property
Private Sub CommandButton1_Click()
Dim lBox As MSForms.ListBox
Dim lbl As MSForms.Label
Dim ctrl As Control
Debug.Print Me.NamePickerCombo.Value, Me.TextBox1.Value
For Each ctrl In ParentForm.Controls
If TypeName(ctrl) = "Label" Then
If ctrl.Caption = Me.NamePickerCombo.Value Then
Set lbl = ctrl
Exit For
End If
End If
Next ctrl
For Each ctrl In ParentForm.Controls
If ctrl.Left >= lbl.Left And ctrl.Left + ctrl.Width <= lbl.Left + lbl.Width Then
Set lBox = ctrl
Exit For
End If
Next ctrl
lBox.AddItem Me.TextBox1.Value
Unload Me
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub