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
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