2011-08-02 38 views
0

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

你如何构建'lstParty'。你可以使用字典(其中int是列表框中的索引)?这会给你更快的查找 –

+0

在这里你必须找到列表框的索引位置。一旦你发现这个工作完成了,但它需要时间来搜索...所以使用多个线程和一个它返回所需条目的索引第一次胜利。这个wud减少搜索条目的时间。 – Anirudha

回答

0

如果包含一秒的延迟,它只会在用户停止输入搜索列表。使用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