Reputation: 1753
I am using 1 combobox to populate a 2nd combobox. What is happening however, is that the 2nd combobox dropdown is changing and displaying the correct values but when the change occurs, the text property still retains the old value. I am using items.clear() and thought that would also clear the text value of the combobox.
I am using winforms and would be grateful if someone could point out my newbie error. Thanks
Private Sub cmbCustomer_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbCustomer.SelectedIndexChanged
' This is the routine to populate the departments box with values from customer
sql = "SELECT * from Departments WHERE Customer = ?"
Dim cmd As New OleDb.OleDbCommand
cmd.Parameters.AddWithValue("@p1", cmbCustomer.Text)
cmd.CommandText = sql
cmd.Connection = oledbCnn
dr = cmd.ExecuteReader
cmbDept.Items.Clear()
cmbRequestBy.Items.Clear()
While dr.Read()
cmbDept.Items.Add(dr("Name"))
End While
cmd.Dispose()
dr.Close()
'oledbCnn.Close()
End Sub
Upvotes: 1
Views: 2490
Reputation: 216293
You are clearing the Items collection not the Text property of the combo.
This behavior depends on the DropDownStyle
property of the combobox.
The default is DropDown
that allow the combo to maintain separate inner controls for Items (A List of some kind) and Text (a TextBox)
If you change the property to DropDownList
clearing the Items collection will clear also the 'text' part of it.
Of course you loose the possibility to type inside the TextBox portion of the combobox for example to add an item not included in the list
See Microsoft Connect for Microsoft Response to similar question
Upvotes: 2