inherited combobox control

PaulR

New member
Joined
Nov 28, 2007
Messages
2
Programming Experience
1-3
I am trying to create a simple inherited combobox control that has an items collection containing all of the US states. The control is working but has one problem. Each of the states are displayed twice during program execution. The following code snippet shows how I am populating the items collection of the control:

VB.NET:
Public Class StateCombobox
    Inherits ComboBox

    Public Sub New()
        MyBase.New()

        MyBase.Items.Add("AL")
        MyBase.Items.Add("AR")
        MyBase.Items.Add("AZ")
        MyBase.Items.Add("CA")
        MyBase.Items.Add("CO")
        MyBase.Items.Add("CT")
        MyBase.Items.Add("DE")
        …
        …
        MyBase.Items.Add("WV")
        MyBase.Items.Add("WI")
        MyBase.Items.Add("WY")

        MyBase.MaxLength = 2
        MyBase.Sorted = True
        MyBase.AutoCompleteMode = Windows.Forms.AutoCompleteMode.Suggest
        MyBase.AutoCompleteSource = Windows.Forms.AutoCompleteSource.ListItems
    End Sub
Any help will be appreciated.
 
Last edited by a moderator:
how do you declare, using this class? what is the code in the app like?

one way to debug this is to put a break in the sub new() and step through while watching the stack window and see how this class is being call.

a bad, quick fix (that I would not do), would be to clear out the combo box before you add any item.
 
Thanks Brian for your reply.

The StateCombobox component was coded in a class library that contains one other control. I compiled the class library as a “.dll” so that I can add each of my controls to a form from the toolbox. When I add the StateCombobox control to a form from the toolbox and select the items collection from the properties list the collection list is correct. When I run the application using the debugger and click the drop down of the combo box control the items list displays each item twice. If I stop the debugger and remove all the items listed in the components item collection and rerun the debugger the drop down of the combo box will display correctly. It appears that the IDE is calling sub new() when the control is created in the IDE designer and then calls it again when the form is actually running in the debugger.

I am new to creating my own controls and had trouble debugging the control from the class library that I created. I keep getting an exception stating that the “.dll” does not contain any UserControl types. I copied the StateCombobox class into a new application and declared the class in a forms load event using the following code:

Private Sub Test_Control_Form_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim test As New StateCombobox
Me.Controls.Add(test)
End Sub

When running the application the combobox worked correctly. I added the break to the sub new() of the StateCombobox class and watched each of the items added only once. The class is working correctly but not as a drop-in component from the toolbox to the IDE form designer. Hopefully my explanation makes sense and answered your question.

Thanks again!
 
Back
Top