文本更改属性上的选定索引值 - 性能命中

时间:2011-08-02 20:36:35

标签: windows vb.net

我正在使用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

1 个答案:

答案 0 :(得分:0)

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

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