vb.netwinformscombobox

Get Selected Item From Combobox


I have a combobox with items from a DataTable, the ff executes when the form loads:

dbConnection = New SqlCeConnection("Data Source=Journal.sdf")
dbDataAdapter = New SqlCeDataAdapter("SELECT * FROM setting_unit", dbConnection)
dbDataAdapter.Fill(dbTable)
cbxSettingsUnit.DataSource = New BindingSource(dbTable, Nothing)
cbxSettingsUnit.DisplayMember = "name"
cbxSettingsUnit.ValueMember = "name"

The method when there is a changed in the combox box:

Private Sub cbxSettingsUnit_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbxSettingsUnit.SelectedIndexChanged
   tempString = cbxSettingsBusinessUnit.Items(cbxSettingsBusinessUnit.SelectedItem).ToString
   MessageBox.Show(tempString)
End Sub

there is an error at the line:

tempString = cbxSettingsBusinessUnit.Items(cbxSettingsBusinessUnit.SelectedItem).ToString

How do I get the selected item from the combox box?


Solution

  • try this:

    combo.datasource = datatable
    combo.displaymember = "abbreviation"
    combo.valuemember = "abbreviation"
    

    then getting the selected item use this:

    on SelectionChangeCommitted event of combo box put this:

        tmpString=combo.selectedvalue
        msgBox(tmpString)