Combo Box in Vb.Net

Hello,
I have one combobox in my form in vb.net 2005. In that combobox I am adding all Personnames from database using form1_load event.
Now the problem is,

If I type some character in combobox then it should check the entire data in database or combobox and the data should be sorted out according to the character which i typed.I had tried the following code:

Private Sub cboPersonname_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cboPersonname.KeyPress
Dim strName As String
Dim intIdx As Integer
strName = cboPersonname.Text
cboPersonname.Items.Clear()
strQuery = "Select * from Customer where Personname like '" & strName & "%' "
db.opentable(strQuery)
For intIdx = 1 To db.dtable.Rows.Count - 1
cboPersonname.Items.Add(New DataDescription(db.dtable.Rows(intIdx).Item("Compn o"), db.dtable.Rows(intIdx).Item("Personname")))
Next
End Sub

The following problem is occuring in my code:

If I type the first character for example consider 'a', then the value passing to the variable strName = "".

If I type the first character for example consider 'ab', then the value passing to the variable strName = a. After this the data are sorted with the string "a" only. not with "ab"

If I type the first character for example consider 'aba', then the value passing to the variable strName = ba. After this the data are sorted with the string "ba" only. not with "aba"

Query and Databaseconnection and Loading Personname with its id are working proerly.Only thing Sorting of data.

That is the combo box ommitted the first character.How can I overcome the Problem?
[1762 byte] By [g1m0v0n5] at [2007-11-20 10:37:04]
# 1 Re: Combo Box in Vb.Net
First up, your problem occurs because the KeyPress event is raised BEFORE the Text changes. You are supposed to be able to reject or change characters in the KeyPress event, so there's not much point raising it after the Text has already changed. You would use the TextChanged event, not the KeyPress event.

That said, performing a query every time the user enters a character is not a good idea. You shouldn't be hitting the database so frequently and you'll also end up getting the same data over and over again. A far better idea would be to just get all the data in the first place. You can then either keep all data displayed at all times and use the auto-complete functionality of the ComboBox control to prompt the user as they type, or else you could simply filter the data to remove all items that don't match.
jmcilhinney at 2007-11-10 3:08:38 >