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!
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!