Reputation:
I currently have two ComboBoxes in my form. ComboBox A is used for the selection of a certain item. ComboBox B will populate with 4 items that are specific to the selection that is made in ComboBox A. I was able to get all of that working.
My problem is: If I make a selection in ComboBox A, ComboBox B will populate with the 4 items specific to the selection I made in ComboBox A. But if I make another selection in ComboBox A, the items do not overwrite the previous items that were populated in ComboBox B, they are just added to the items that are already populated.
My question is, is there anyway to overwrite the previous itemsin ComboBox B when a new selection is made in ComboBox A?
Imports MySql.Data.MySqlClient
Public Class Form2
Dim MySqlConn As MySqlConnection
Dim COMMAND As MySqlCommand
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MySqlConn = New MySqlConnection
MySqlConn.ConnectionString = "server=localhost;userid=root;password=root;database=test"
Dim READER As MySqlDataReader
Try
MySqlConn.Open()
Dim Query As String
Query = "select * from test.boxinformation"
COMMAND = New MySqlCommand(Query, MySqlConn)
READER = COMMAND.ExecuteReader
While READER.Read
Dim boxSN = READER.GetString("Box_SN")
ComboBox_Box.Items.Add(boxSN)
End While
MySqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MySqlConn.Dispose()
End Try
End Sub
Private Sub ComboBox_STSBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox_Box.SelectedIndexChanged
MySqlConn = New MySqlConnection
MySqlConn.ConnectionString = "server=localhost;userid=root;password=root;database=test"
Dim READER As MySqlDataReader
Try
MySqlConn.Open()
Dim Query As String
Query = "select * from test.boxinformation where Box_SN='" & ComboBox_Box.Text & "'"
COMMAND = New MySqlCommand(Query, MySqlConn)
READER = COMMAND.ExecuteReader
While READER.Read
ComboBox_Port.Items.Add(READER.GetString("1_IP"))
ComboBox_Port.Items.Add(READER.GetString("2_IP"))
ComboBox_Port.Items.Add(READER.GetString("3_IP"))
ComboBox_Port.Items.Add(READER.GetString("4_IP"))
End While
MySqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MySqlConn.Dispose()
End Try
End Sub
End Class
Upvotes: 0
Views: 129
Reputation: 33682
In order to celan you ComboBox from previous values use ComboBox_Port.Clear
, put it after your line
READER = COMMAND.ExecuteReader
So your code will be:
READER = COMMAND.ExecuteReader
' *** drop your new line here ***
ComboBox_Port.Clear
While READER.Read
ComboBox_Port.Items.Add(READER.GetString("1_IP"))
ComboBox_Port.Items.Add(READER.GetString("2_IP"))
ComboBox_Port.Items.Add(READER.GetString("3_IP"))
ComboBox_Port.Items.Add(READER.GetString("4_IP"))
End While
Upvotes: 0
Reputation: 5477
if you add new items without first removing the old ones then this would be expected behaviour. To remove all items from your combobox before you add your new set you would need to clear its objectCollection like this;
ComboBoxA.ObjectCollection.Clear()
'now add your new set of items...
Upvotes: 0