【问题标题】:Selected Index value on Text Change Property - Performance hit文本更改属性上的选定索引值 - 性能影响
【发布时间】:2011-08-03 07:18:37
【问题描述】:

我正在处理 Windows 应用程序表单,我有一个文本框和列表框。我想如果用户在文本框上输入,那么列表框项目将被选中,这工作正常。列表框有超过 10,000 条记录。

从ListBox中选择项目,在文本框中写入数据需要时间。

这是我的代码:

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If TextBox1.Text.Length > 0 Then
            Dim iSelectedInd As Int32
            iSelectedInd = lstParty.FindString(TextBox1.Text)
            If iSelectedInd >= 1 Then
                lstParty.SetSelected(iSelectedInd, True)
            End If
        End If
End Sub

【问题讨论】:

  • 你如何构建lstParty。您可以使用 Dictionary (其中 int 是列表框中的索引)?这将使您更快地查找
  • 在这里你必须找到条目所在的 ListBox 的索引。一旦你找到这个你的工作就完成了..但是搜索需要时间......所以使用多个线程和返回所需条目索引的第一个获胜..因此这减少了搜索条目的时间。

标签: windows vb.net


【解决方案1】:

如果您包含一秒钟的延迟,它只会在用户停止输入时搜索列表。使用Interval = 1000Enabled = False 创建Timer

Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox1.TextChanged
    ' Reset the timer.
    Timer1.Enabled = False
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
    ' Stop the timer.
    Timer1.Enabled = False
    ' Search the list for the text.
    If TextBox1.Text.Length > 0 Then
        Dim iSelectedInd As Int32
        iSelectedInd = lstParty.FindString(TextBox1.Text)
        If iSelectedInd >= 1 Then
            lstParty.SetSelected(iSelectedInd, True)
        End If
    End If
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-16
    • 1970-01-01
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    相关资源
    最近更新 更多