سؤال

How can I achieve the search functionality that works with multiple keys in CheckedListBox and ListBox?

Example: I have the following items in the list:

1000 1500 1520 2010 5001 5102

When I press the key 1, instantly search the first hit that match with the first char that starts with '1'.

But, If I want locate the item "5102", the list only will can search the 5, and then, I need go manually to identify the wanted item.

هل كانت مفيدة؟

المحلول

Answering to my own question. This it's a clean solution that doesn't need too much work (All the code can handle in the KeyDown event):

Sub DropDownListBox_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
    If Char.IsLetterOrDigit(Chr(e.KeyValue)) Then
        e.SuppressKeyPress = True
        If TryCast(sender.tag, Timer) Is Nothing Then
            Dim vTimer As New Timer

            vTimer.Interval = 1000
            vTimer.Tag = ""
            sender.Tag = vTimer

            AddHandler vTimer.Tick,
                Sub(Timer As Object, eventsArgs As System.EventArgs)
                    Timer.Tag = ""

                    If Not TryCast(sender, CheckedListBox).Visible Then
                        Timer.dispose() : Timer = Nothing
                    End If
                End Sub
        Else
            sender.Tag.Stop() : sender.Tag.Start()
            sender.Tag.Tag &= Chr(e.KeyValue)

            Dim vIndex As Integer = TryCast(sender, CheckedListBox).FindString(sender.Tag.Tag)

            If vIndex <> -1 Then TryCast(sender, CheckedListBox).SelectedItem = TryCast(sender, CheckedListBox).Items(vIndex)
        End If
    End If
End Sub

Basically I use the TAG object to keep a Timer running every 1 sec; then, if the user typing several chars, the process will find exactly the wanted text.

Any comment or feedback will be welcome.

نصائح أخرى

Public Class Form1
    Dim Type As String
    Private Sub ListBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles ListBox1.KeyPress
        If Asc(e.KeyChar) <> 8 Then
            If Asc(e.KeyChar) < 65 Or Asc(e.KeyChar) > 122 Then
                e.Handled = True
                ' Clear String at Escape key press & Reset Listbox location
                If Asc(e.KeyChar) = 27 Then
                    Type = ""
                    ListBox1.SelectedIndex = -1
                    ListBox1.SelectedItem = ListBox1.SelectedIndex
                End If
            End If
            If Asc(e.KeyChar) = 27 Then
                ' Do not add escape key char to string at key press
            Else
                ' add char one by one after key press
                Type &= e.KeyChar
                Dim TL As Integer = Type.Length
                Dim i As Integer = 0
                For i = 0 To ListBox1.Items.Count - 1
                    ' match key press in total items in listbox
                    'MsgBox(Format$(Mid$(ListBox1.Items(i), 1, TL)))
                    If Format$(Mid$(ListBox1.Items(i), 1, TL)) = Type Then
                        ListBox1.SelectedIndex = i
                    End If
                Next
            End If
        End If
    End Sub
End Class
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top