Results 1 to 7 of 7

Thread: [RESOLVED] Correct ComboBox filtering

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2012
    Posts
    616

    Resolved [RESOLVED] Correct ComboBox filtering

    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?

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,374

    Re: Correct ComboBox filtering

    With out really studying your code, I can see right off hand what's your most probable cause for the flickering... The Do...Loop...Until loop. Is there any way that you can restructure it to For...Next loop? Because what if bMatchFound never happens? It will stay in an infinite loop. Also, I noticed that you're using Mid(), that should be replaced by getting the SubString.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2012
    Posts
    616

    Re: Correct ComboBox filtering

    Quote Originally Posted by dday9 View Post
    With out really studying your code, I can see right off hand what's your most probable cause for the flickering... The Do...Loop...Until loop. Is there any way that you can restructure it to For...Next loop? Because what if bMatchFound never happens? It will stay in an infinite loop. Also, I noticed that you're using Mid(), that should be replaced by getting the SubString.
    Yes... the code probably stays trapped into a loop.
    About for...next loop, I dunno, Theoretically, possible but it asks to be combine with If...End if, I think. For...Next is "one-dimensional" loop and it wouldn't be so easy to implement.

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Correct ComboBox filtering

    Might one ask why you're not using the perfectly serviceable autocomplete options built in to the combobox rather than attempting this rather strange and ever so slightly bonkers method? I can't see how the loop you're using ever resolves itself as it simply reads exactly the same values over and over again (to say nothing of the fact that it appears to be seeking whole texts but is activated by a single key press.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2012
    Posts
    616

    Re: Correct ComboBox filtering

    Because I don't know that it exists! After a lot of googling I didn't find anything smarter.
    BTW, this is .NET v2.0, VS05

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Correct ComboBox filtering

    .NET v2.0, VS05
    Might be helpful to start with that next time. You'll need someone with either a very good memory or a degree in Ancient History to be definitive about solutions for that! Is there no prospect of joining us in this decade code wise?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2012
    Posts
    616

    Re: Correct ComboBox filtering

    VB Code:
    1. Public Sub AutoComplete2(ByVal cbo As ComboBox, ByVal e As KeyEventArgs)
    2.         Dim sTypedText As String
    3.         Dim iFoundIndex As Integer
    4.         Dim oFoundItem As Object
    5.         Dim sFoundText As String
    6.         Dim sAppendText As String
    7.         Select Case e.KeyCode
    8.             Case Keys.Back, Keys.Left, Keys.Right, Keys.Up, Keys.Delete, Keys.Down
    9.                 Return
    10.         End Select
    11.         sTypedText = cbo.Text
    12.         iFoundIndex = cbo.FindString(sTypedText)
    13.         If iFoundIndex >= 0 Then
    14.             oFoundItem = cbo.Items(iFoundIndex)
    15.             sFoundText = cbo.GetItemText(oFoundItem)
    16.             sAppendText = sFoundText.Substring(sTypedText.Length)
    17.             cbo.Text = sTypedText & sAppendText
    18.             cbo.SelectionStart = sTypedText.Length
    19.             cbo.SelectionLength = sAppendText.Length
    20.         End If
    21.     End Sub

    VB Code:
    1. Public Sub AutoCompleteCombo_Leave(ByVal cbo As ComboBox)
    2.         Dim iFoundIndex As Integer
    3.         iFoundIndex = cbo.FindStringExact(cbo.Text)
    4.         cbo.SelectedIndex = iFoundIndex
    5.     End Sub

    =====================


    vb Code:
    1. Private Sub cbox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
    2.         AutoComplete2(cbox1, e)
    3.     End Sub

    =====================

    This one works fine

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width