Question Tab key does not set Combobox.selectedindex with autocomplete

phanks

New member
Joined
Mar 25, 2009
Messages
4
Programming Experience
1-3
I am having a problem to get the results of a combobox when the user uses the tab key to exit the combobox. The combobox uses a database as the datasource. When the user uses the mouse to click on the selection the selectedindex is set proprerly, however when the user will enter some text and presses the tab key, selectedindex is set to -1.

I have attached some test code to duplicate the issue. Any help is appreciated.

Imports System.Data.OleDb

Public Class Form1
Protected dvNames As DataView
Dim conDBTest As OleDbConnection
Dim strCMD As OleDbCommand
Dim odaDBTest As OleDbDataAdapter
Dim dsContacts As DataSet

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.DbContactsTableAdapter.Fill(Me.DbTest1DataSet.dbContacts)
conDBTest = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=C:\Data\phanks\My Documents\dbTest1.mdb")
If conDBTest.State = ConnectionState.Closed Then conDBTest.Open()
dvNames = New DataView(Me.DbTest1DataSet.dbContacts, "", "Name ASC", DataViewRowState.OriginalRows)
cboNameBuildList()
End Sub

Private Sub cboNameBuildList()
With Me.cboName
.AutoCompleteMode = AutoCompleteMode.SuggestAppend
.AutoCompleteSource = AutoCompleteSource.ListItems
.DataSource = dvNames ' use the dataview created earier
.DisplayMember = "Name" ' what field to display
.ValueMember = "ID" ' what value to use
.Text = "Select Name" ' set the default text displayed
.SelectedIndex = 0 ' start at the first data element
.DropDownWidth = AutoScaleMode ' autofit the data in the dropdown
End With
End Sub


Private Sub cboName_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboName.Leave
GetContactInfo()
End Sub

Private Sub cboName_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboName.SelectedValueChanged
If Me.cboName.Focused Then GetContactInfo()
End Sub

Private Sub cboName_DropDownClosed(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboName.DropDownClosed
Me.txAddress.Text = cboName.SelectedValue.ToString
GetContactInfo()
End Sub

Private Sub GetContactInfo()
Dim strSQL As String

strSQL = "Select Name, Address, City, St, Zip FROM dbContacts WHERE ID = " & _
cboName.SelectedValue.ToString
strCMD = New OleDbCommand(strSQL, conDBTest)
odaDBTest = New OleDbDataAdapter(strCMD)
dsContacts = New DataSet()
odaDBTest.Fill(dsContacts, "tblTemp1")
DataGridView1.DataSource = dsContacts.Tables("tblTemp1").DefaultView
DataGridView1.Visible = True
DataGridView1.RowHeadersVisible = False
End Sub

Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
conDBTest.Close()
conDBTest = Nothing
odaDBTest = Nothing
dsContacts = Nothing
End Sub

End Class
 
When the DropDownStyle is set to DropDownList then the user can only select items from the list. If you only want them to select items from the list then that's what you should do.

If you set the DropDownStyle to DropDown then the user can either select an item from the list or enter their own text, which can be anything. If the user enters their own text then they haven't selected an item from the list, so the SelectedIndex will reflect that. When the user enters text that matches one of the items in the list, if you want that to have the effect of selecting that item then it's up to you to do it yourself. In that case you would handle either the Validated or Leave event, whichever is more appropriate in your case, and test the Text to see whether it matches one of the list items. If it does (and you'd probably want to perform a case-insensitive comparison, but maybe not) then you would select that item.
 
Back
Top