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!




Reply With Quote