|
-
Apr 19th, 2006, 03:21 AM
#1
Thread Starter
Lively Member
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:
Private Sub txtProject_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtProject.KeyUp
Dim max As Integer = lstProject.Items.Count
Dim tempstr() As String
Dim i As Integer = 0
Dim len As Integer = txtProsjekt.Text.Length
[INDENT]While i < max
[INDENT]tempstr = Split(lstProject.Items(i).ToString, vbTab)
If tempstr(1).ToLower.StartsWith(txtProject.Text.ToLower) Then
[INDENT]lstProject.SelectedIndex = i
txtProject.Text = tempstr(1)
txtProject.Select(len, 25) 'maximum length in this field is 25
Exit Sub[/INDENT]End If
i = i + 1[/INDENT]
End While[/INDENT]
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
-
Apr 21st, 2006, 09:42 PM
#2
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:
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Me.ListBox1.SelectedIndex = Me.ListBox1.Items.IndexOf(Me.TextBox1.Text)
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:
Dim arr(Me.ListBox1.Items.Count - 1) As String
For i As Integer = 0 To arr.GetUpperBound(0)
arr(i) = Me.ListBox1.GetItemText(Me.ListBox1.Items(i)).Split(ControlChars.Tab)(1)
Next i
Me.TextBox1.AutoCompleteCustomSource.Clear()
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.
-
May 2nd, 2006, 09:08 AM
#3
Thread Starter
Lively Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|