开发者

Selected Index value on Text Change Property - Performance hit

I am working on win开发者_StackOverflow中文版dows application form I am having one text box and Listbox. I want if user type on textbox, then List box item is going to be selected, that is working fine. List Box has more than 10,000 records.

It takes time to select item from ListBox, while write data in textbox.

Here is my code:

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


If you include a one second delay, it'll only search the list if the user stops typing. Create a Timer with Interval = 1000 and 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜