Results 1 to 3 of 3

Thread: Selecting in listbox while user input being typed

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    69

    Resolved Selecting in listbox while user input being typed

    Hi all
    (using VB.NET 2005)
    The idea of the code below is to select the correct item from a listbox while the user types a name in a textbox. So if he Types "He" then the sub below is supposed to select the item in the itemlist that starts with "He" (well actually it doesnt start with "He" because the list consists of [projectnumber] [projectname], hence the split() ), it also makes the rest of the text selected. So if the name in the list is "Hennes" then "Hennes" would be copied to the textbox and "nnes" would be the selected text.

    VB Code:
    1. Private Sub txtProject_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtProject.KeyUp
    2.         Dim max As Integer = lstProject.Items.Count
    3.         Dim tempstr() As String
    4.         Dim i As Integer = 0  
    5.         Dim len As Integer = txtProsjekt.Text.Length
    6. [INDENT]While i < max
    7. [INDENT]tempstr = Split(lstProject.Items(i).ToString, vbTab)
    8. If tempstr(1).ToLower.StartsWith(txtProject.Text.ToLower) Then
    9. [INDENT]lstProject.SelectedIndex = i
    10. txtProject.Text = tempstr(1)
    11. txtProject.Select(len, 25)   'maximum length in this field is 25
    12. Exit Sub[/INDENT]End If
    13. i = i + 1[/INDENT]
    14. End While[/INDENT]
    15. End sub

    The code itself works very well as long as the user doesnt type too fast. And that is the problem i was hoping You could help me solve. The code doesnt work fast enough so if i type very fast the textbox would look like this:
    "Hennes e" (where i type "He"). I guess i could lock the textbox during the search, but that i think would only make the program seem slow.

    Any help here would be greatly appriciated!
    Last edited by Riks; May 2nd, 2006 at 09:08 AM.
    2B || !2B

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Selecting in listbox while user input being typed

    You should look into using autocomplete. The TextBox control in .NET 2.0 has properties named AutoCompleteMode, AutoCompleteSource and AutoCompleteCustomSource. I just tried simulating a situation similar to yours and it worked seemlessly using those properties. I set the AutoCompleteMode to Append, the AutoCompleteSource to CustomSource and then added all the items from a ListBox to the AutoCompleteCustomSource collection. I then added this event handler:
    VB Code:
    1. Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    2.         Me.ListBox1.SelectedIndex = Me.ListBox1.Items.IndexOf(Me.TextBox1.Text)
    3.     End Sub
    You'll have to do a little more work because of the fact that your ListBox doesn't contain the exact values that the user will be typing but you get the idea. One thing to note is that the Contains method is case-sensitive so typing the incorrect case didn't stop the autocomplete but it did prevent the corresponding item in the ListBox being selected. If you want to refresh the AutoCompleteCustomSource collection based on the contents of your ListBox you would this:
    VB Code:
    1. Dim arr(Me.ListBox1.Items.Count - 1) As String
    2.  
    3.         For i As Integer = 0 To arr.GetUpperBound(0)
    4.             arr(i) = Me.ListBox1.GetItemText(Me.ListBox1.Items(i)).Split(ControlChars.Tab)(1)
    5.         Next i
    6.  
    7.         Me.TextBox1.AutoCompleteCustomSource.Clear()
    8.         Me.TextBox1.AutoCompleteCustomSource.AddRange(arr)
    Having said all that, I would advise you to not use a ListBox at all. ListBoxes are designed for list of individual items and are not the best for tabular data. If your data has more than one column I would suggest using a ListView in Details View instead, or perhaps even a DataGridView.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    69

    Re: Selecting in listbox while user input being typed

    Thank you for your reply, and sorry for my late reply...
    Ive been looking into the autocomplete-mode and that seems to do the job well. I took your advise tho so i no longer use Listbox (swapped to a Treelistview by DevExpress).

    Thanks again for your reply
    2B || !2B

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