StBlade StBlade - 6 months ago 26
Vb.net Question

VB Combobox not reseting data

I am sitting with a problem that has me stumped. I am coding a VB app where a users enters stock into an array on one form. On another form I have a combobox that gets populated from this array when the user clicks on the ComboBox. Now everytime that I click on the dropdown for the combobox another item gets added to the combobox. Currently there is only one item in the array. If I add another item to the array, the amount of items is doubled on each click.

Private Sub cmbStock_Click(sender As Object, e As EventArgs) Handles cmbStock.Click
cmbStock.ResetText()
cmbStock.SelectedIndex = -1
cmbStock.Items.Add(ProdDesc(0))
End Sub


I have tried to use the ResetText(), thinking that everytime the user clicks on the ComboBox it will reset and then load again. Not hapening.

I have tried changing the sub to cmbStock_OnFocus(), but the same happens as above. I have even tried to force it to show just one item from the array(as above)

It seems it should work, only other thing that I can think of is to write more code that will test to see if anything is added before adding more items.

Does anyone have a better solution, please??

Answer

You are adding item on every click without checking if that item is actually in the combo box. You should check for before existence of the same item and if not then add it.

Private Sub cmbStock_Click(sender As Object, e As EventArgs) Handles cmbStock.Click
        cmbStock.ResetText()
        cmbStock.SelectedIndex = -1
        If Not cmbStock.Items.Contains(ProdDesc(0)) Then
            cmbStock.Items.Add(ProdDesc(0))
        End If
    End Sub
Comments