Preventing items from being added automatically to a comboBox

L

Linda

Greetings!

In order to keep better track a form with several combo-boxes, I've
subclassed the ones that need to change in response to events on other
controls on the form. An example of a subclassed combo box:

<code>
Public Class byte13ComboBox
Inherits System.Windows.Forms.ComboBox
Private comboItems() As String = {"a (Numbered)", _
"b (Unnumbered)", _
"c (Numbering varies)", _
"n (Not applicable)"}
Sub New()
MyBase.New()

For Each j As Object In Items
Items.Remove(j)
Next

For Each i As String In comboItems
Items.Add(i)
Next

SelectedIndex = 0
' The user must choose an item already in the list.
DropDownStyle = ComboBoxStyle.DropDownList
End Sub

'...Various custom functions removed for clarity

End Class
</code>

The problem is that even though I empty the Items Collection each time,
the items build up: the first time I run the program, the Combo works
properly. The next time I run it, it has eight options, the next time
twelve, etc.

When I look at the Items property on the Forms view, I can see that
these items are being added cumulatively there. Emptying it on the
Forms view resets it, but the list items immediately start accumulating
again.

How do I prevent VB from automatically adding extra items? Or is there
an event I should be using to reset the ComboBox, rather than doing it
in the constructor?

Thanks!
 
L

Linda

Crouchie1998,

I tried that, and it didn't work, so I added the removal code.

But I later realized that wasn't the problem, because the Items were
being added back in *after* I cleared it, but I had forgotten to change
it back to clear(). So the problem still remains: even if I empty the
Items in the constructor, they still build up cumulatively each time I
run the form.

Thanks,

Linda
 
L

Linda

Hi,

I got around the problem by pulling the code out of the contsructor,
and putting it into a "Reset()" function which I call explicitly on
Form_Load. Handling it in the constructor would have been more elegant,
but this works too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top