Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27412

Correct ComboBox filtering

$
0
0
Here's a code which should filter and show ComboBox items according to our input,

Code:

Public Sub CboxAutoComplete(ByVal cbo As ComboBox, ByVal e As System.Windows.Forms.KeyEventArgs)
       
       
        Dim iIndex As Integer
        Dim sActual As String
        Dim sFound As String
        Dim bMatchFound As Boolean

        If e.KeyCode = Keys.Back Then
            'cbo.Text = Mid(cbo.Text, 1, Len(cbo.Text) - 1)
            cbo.Text = ""
        End If

        If ((e.KeyCode = Keys.Left) Or _
        (e.KeyCode = Keys.Right) Or _
        (e.KeyCode = Keys.Up) Or _
        (e.KeyCode = Keys.Down) Or _
        (e.KeyCode = Keys.PageUp) Or _
        (e.KeyCode = Keys.PageDown) Or _
        (e.KeyCode = Keys.Home) Or _
        (e.KeyCode = Keys.End)) Then
            Return
        End If
        Do

            sActual = cbo.Text
            iIndex = cbo.FindString(sActual)
            'if index > -1 then a match was found.
            If (iIndex > -1) Then
                sFound = cbo.Items(iIndex).ToString()
                cbo.SelectedIndex = iIndex
                cbo.SelectionStart = sActual.Length
                cbo.SelectionLength = sFound.Length
                bMatchFound = True
            Else

                If sActual.Length = 1 Or sActual.Length = 0 Then
                    cbo.SelectedIndex = 0
                    cbo.SelectionStart = 0
                    cbo.SelectionLength = Len(cbo.Text)
                    bMatchFound = True
                Else

                    cbo.SelectionStart = sActual.Length - 1
                    cbo.SelectionLength = sActual.Length - 1
                    cbo.Text = Mid(cbo.Text, 1, Len(cbo.Text) - 1)
                End If
            End If
        Loop Until bMatchFound
    End Sub

Unfortunately, this code have some bugs inside which blocks VS under some circumstances. This simply doesn't work well enough as we'd expect from filtered combo box.

Of course, this code have to be called from an event like this:

Code:

Private Sub cbox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cboINtervencija3.KeyDown
        Call CboxAutoComplete(cbox1, e)

I've tried with debugging but without success. Any idea?

Viewing all articles
Browse latest Browse all 27412

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>