将信息添加到动态列表框中

时间:2014-08-20 07:58:59

标签: vba excel-vba excel

在我的用户表单中,我有一个问题:

Number of Students?

无论用户输入什么数字,他们都会收到一个动态文本框&每个号码的列表框(因此,如果用户输入2,他们将收到动态文本框和列表框+另一个动态文本框和列表框)。我当时想要做的是将输入到动态文本框中的数字插入到其相应的列表框中,但我无法使其工作。下面显示了我到目前为止的代码:

Option Explicit

Dim TextBox() As New Class3
Dim ListBox() As New Class3

Private Sub TextBox1_Change()
Dim i As Integer
Dim buttonStartPosition As Integer
Dim BtxtStartPosition As Integer
Dim listStartPosition As Integer
Dim BlabelStartPosition As Integer
Dim newPosition As Integer
Dim cButton As CommandButton
Dim cText As Control
Dim cList As Control

buttonStartPosition = 30
BtxtStartPosition = 270
listStartPosition = 290

If TextBox1 <> vbNullString Then
    For i = 1 To TextBox1.Value
    newPosition = 200

    Set cText = Me.Controls.Add("Forms.TextBox.1")
        With cText
            .Name = "breakerText" & (i)
            .Left = 174
            .Top = BtxtStartPosition
            .Width = 72
            .Height = 15.75
    End With

    ReDim Preserve TextBox(1 To i)
    Set TextBox(i).TextGroup = cText
    BtxtStartPosition = BtxtStartPosition + newPosition

    Set cList = Me.Controls.Add("Forms.ListBox.1")
        With cList
            .Name = "listbox_" & (i)
            .Left = 150
            .Top = listStartPosition
            .Width = 300
            .Height = 140
        End With

    ReDim Preserve ListBox(1 To i)
    Set ListBox(i).ListGroup = cList
    listStartPosition = listStartPosition + newPosition

然后我有了为动态控件执行功能的类:

Public WithEvents ListGroup As MSForms.ListBox
Public WithEvents TextGroup As MSForms.TextBox

Private Sub TextGroup_Change()
If TextGroup.Name = "breakerText1" Then
 'Insert into listbox1

ElseIf TextGroup.Name = "breakerText2" Then
'Insert into listbox2

End If

End Sub

如果有人能指出我在正确的方向,我将非常感激,我只需要将动态文本框值填充到相应的文本框中。下面显示了用户表单的图像,以便更好地理解该事项。

enter image description here

1 个答案:

答案 0 :(得分:1)

例如:

用户形式代码:

Option Explicit

Dim TextListBox() As New Class3

Private Sub TextBox1_Change()
Dim i As Integer
Dim buttonStartPosition As Integer
Dim BtxtStartPosition As Integer
Dim listStartPosition As Integer
Dim BlabelStartPosition As Integer
Dim newPosition As Integer
Dim cButton As CommandButton
Dim cText As Control
Dim cList As Control

buttonStartPosition = 30
BtxtStartPosition = 270
listStartPosition = 290

If TextBox1 <> vbNullString Then
    ReDim TextListBox(1 To TextBox1.Value)
    For i = 1 To TextBox1.Value
    newPosition = 200

    Set cText = Me.Controls.Add("Forms.TextBox.1")
        With cText
            .Name = "breakerText" & (i)
            .Left = 174
            .Top = BtxtStartPosition
            .Width = 72
            .Height = 15.75
    End With

    Set TextListBox(i).TextGroup = cText
    BtxtStartPosition = BtxtStartPosition + newPosition

    Set cList = Me.Controls.Add("Forms.ListBox.1")
        With cList
            .Name = "listbox_" & (i)
            .Left = 150
            .Top = listStartPosition
            .Width = 300
            .Height = 140
        End With

    Set TextListBox(i).ListGroup = cList
    listStartPosition = listStartPosition + newPosition
    Next i
End If
End Sub

Class3代码

Option Explicit

Public WithEvents ListGroup As MSForms.ListBox
Public WithEvents TextGroup As MSForms.TextBox

Private Sub TextGroup_Change()
    ListGroup.AddItem TextGroup.Text
End Sub

但是,因为您使用更改事件,所以您将在列表中为文本框中的每个击键添加一个项目。由于您不能使用带有WithEvents控件的常用Exit或Before / After_Update事件,我建议您使用命令按钮。