UserForm文本框速度慢,并且没有显示键入的字符

时间:2019-04-10 22:05:24

标签: excel vba listbox userform

我试图在用户窗体中获得一个列表框,以通过在同一窗体上键入文本框的内容来动态过滤内容。它可以工作,但是当用户开始键入代码时,它基本上会冻结,同时执行要过滤的代码。如果我快速键入一系列字符,则文本框将在几秒钟内不显示字符。我想动态过滤列表框而不会出现滞后。

我尝试创建一个计时器,该计时器对用户自从在执行过滤器之前最后一次键入字符以来所经过的时间进行计时,但是这带来了另一组问题,包括用户键入的最后一个字符未触发textbox_change(如果相对于倒数第二个字符快速键入)。

Private Sub FilterTextBox_Change()

Dim bSelected As Boolean
Dim lListIncrementer As Long
Dim lDictionaryIncrementer As Long
Dim lOriginalListBoxDictionaryIncrementer As Long
Dim lLastListItem As Long
Dim sFilterString As String
Dim lDictionaryIncrementerOffset As Long
Dim dTimerDifference As Double
Dim sCurrentString As String

sFilterString = UCase(InvoiceProductAssociation.FilterTextBox.Text)
lLastListItem = InvoiceProductAssociation.CatapultItemIdAndDescriptionListBox.ListCount - 1
lDictionaryIncrementerOffset = 0


For lDictionaryIncrementer = 0 To oListBoxItemDictionary.Count - 1
    sCurrentString = UCase(oListBoxItemDictionary.Keys(lDictionaryIncrementer))

    If InStr(sCurrentString, sFilterString) Then
        oListBoxItemDictionary(oListBoxItemDictionary.Keys(lDictionaryIncrementer)) = "Show"
    ElseIf sFilterString = vbNullString Then
        oListBoxItemDictionary(oListBoxItemDictionary.Keys(lDictionaryIncrementer)) = "Show"
    Else
        oListBoxItemDictionary(oListBoxItemDictionary.Keys(lDictionaryIncrementer)) = "Hide"
    End If
Next lDictionaryIncrementer

For lListIncrementer = 0 To lLastListItem

    If InvoiceProductAssociation.CatapultItemIdAndDescriptionListBox.Selected(lListIncrementer) = True Then
        oListBoxSelectedDictionary("Selected") = InvoiceProductAssociation.CatapultItemIdAndDescriptionListBox.List(lListIncrementer)
    End If

Next lListIncrementer

InvoiceProductAssociation.CatapultItemIdAndDescriptionListBox.Clear

For lDictionaryIncrementer = 0 To oListBoxItemDictionary.Count - 1

    If oListBoxItemDictionary.Items(lDictionaryIncrementer) = "Show" Then

        With InvoiceProductAssociation.CatapultItemIdAndDescriptionListBox
            .AddItem oListBoxItemDictionary.Keys(lDictionaryIncrementer)
            If oListBoxSelectedDictionary("Selected") = oListBoxItemDictionary.Keys(lDictionaryIncrementer) Then
                InvoiceProductAssociation.CatapultItemIdAndDescriptionListBox.Selected(lDictionaryIncrementer + lDictionaryIncrementerOffset) = True
            End If
        End With
    Else
        lDictionaryIncrementerOffset = lDictionaryIncrementerOffset - 1
    End If

    lListIncrementer = lListIncrementer + 1

Next lDictionaryIncrementer

End Sub

正在加载到词典中的项目数超过1,000,但是过去我对词典的反应非常灵敏,因此我认为它的.additem会导致滞后。

我想知道是否有人对如何更快地将项目添加到列表框中有任何建议,或者有建议仅在用户停止键入x秒后才成功触发过滤器。

0 个答案:

没有答案