By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,558 Members | 1,673 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,558 IT Pros & Developers. It's quick & easy.

Preventing items from being added automatically to a comboBox

P: n/a
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!

Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Why don't you just do:

'ComboBox1.Items.Clear()'?
Nov 21 '05 #2

P: n/a
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

Nov 21 '05 #3

P: n/a
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.

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.