0

CheckedListBoxとの複数のキーで動作する検索機能を実現するにはどうすればよいListBoxですか?

例: リストに次の項目があります。

1000 1500 1520 2010 5001 5102

キー 1 を押すと、「1」で始まる最初の文字に一致する最初のヒットを即座に検索します。

しかし、アイテム「5102」を見つけたい場合、リストは5つしか検索できず、手動で目的のアイテムを特定する必要があります。

4

2 に答える 2

0

私自身の質問に答えます。これは、あまり多くの作業を必要としないクリーンなソリューションです (すべてのコードは KeyDown イベントで処理できます)。

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

基本的に、TAG オブジェクトを使用して、タイマーを 1 秒ごとに実行し続けます。次に、ユーザーが複数の文字を入力すると、プロセスは目的のテキストを正確に見つけます。

コメントやフィードバックは大歓迎です。

于 2013-02-22T17:17:47.963 に答える
-1
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
于 2016-01-01T12:00:59.323 に答える