Results 1 to 5 of 5

Thread: autocomplete

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Posts
    113

    autocomplete

    I would like to be able to setup a textbox to perform a search for the nearest matching entry in a listbox as the user types in the textbox.

    Can anyone point me to an example like this or even give me some help ?

    Thanks

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    use the change event . I will make sure .

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Posts
    113

    [RESOLVED]

    Problem is resolved.

    I am attaching a demo if anyone would like to review it.
    Attached Files Attached Files

  4. #4
    Member
    Join Date
    Sep 2002
    Location
    California
    Posts
    52
    You have to copy the ListBox items into an array and then match it against regular expressions.

    Here's a simple matching mechanism I quickly wrote up. You can easily add more complexity by toying with the regular expression (I'm not all that familiar with regular expressions myself, but can do simple things with them as shown below).

    Code:
        Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
            'Regular expression to match against.
            Dim exp As String = "^(" + TextBox1.Text.ToLower + ")+"
            Dim r As New System.Text.RegularExpressions.Regex(exp)
    
            Dim s As String 'String for looping.
            Dim list(ListBox1.Items.Count - 1) As String    'Array for our list.
            ListBox1.Items.CopyTo(list, 0)  'Copy list into array.
    
            'Loop through each list item to look for a match.
            For Each s In list
                If r.IsMatch(s.ToLower) Then
                    ListBox1.SelectedItem = s   'Select the matching item.
                    Exit For    'Found match, exit.
                End If
            Next
        End Sub

  5. #5
    Member
    Join Date
    Sep 2002
    Location
    California
    Posts
    52
    kerv21:

    Nice demo.. and I don't mean to be a stickler but it's not good to promote old legacy VB6 coding in a VB.NET forum. Microsoft even suggests staying away from the VisualBasic namespaces as they are not guaranteed support in the future. They are only there to help the transition for old VB6 coders.

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