Adam Goossens said:
Can we see the code for the custom combo box? (Or, at least, the parts
that really count)
It might be a bug in your implementation that's causing the designer to
add that line of code (but I doubt it).
I've never run into this problem before. Then again, I've never
subclassed a combo box before
Okay, here's how it works. I have a class library within which I have
created a custom combobox called EnumCombobox. It inherits from
System.Windows.Forms.Combobox. Now, this EnumCombobox is declared
MustInherit.
I have a second custom combobox class that inherits EnumCombobox. In its
constructor it calls a method of the base class (EnumCombobox) telling it to
populate the combobox.
The point of this arrangement was to create multiple custom comboboxes, one
for each of several custom Enums I have made for my project. The comboboxes
actually display the Enum names with spaces inserted before each capital
letter, or something like that, and have an option to display a blank item.
Here is how one of the second custom comboboxes looks. The only code I
added was the call to Me.Populate() in the constructor. The second
argument, a boolean, tells the Populate() method to insert a blank line. :
--- CODE START ---
Public Class ProcedureToothTypeComboBox
Inherits EnumComboBox
#Region " Component Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Component Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
Me.Populate("VegaBizObj.Enumerations+ProcedureToothType", True)
End Sub
'Component overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Component Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Component Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
components = New System.ComponentModel.Container
End Sub
#End Region
End Class
--- CODE END ---
The Populate() from the base class looks like this. The code calls a
FillList() method which actually puts items in the list. The GetList()
method just retrieves a collection of objects representing the various
members of the Enum:
--- CODE START ---
Friend Sub Populate(ByVal enumName As String, ByVal includeBlankItem As
Boolean)
Dim selectedIndex As Integer = -1
Try
' Remember enum name
_EnumerationName = enumName
_IncludeBlankItem = includeBlankItem
' Prevent control from visually updating
Me.SuspendLayout()
' If an enum name has been specified...
If _EnumerationName.Length > 0 Then
' Clear the list
Me.Items.Clear()
' Fill in the control with enum values
FillList(GetList(_EnumerationName))
End If
Catch ex As Exception
' Log the error
LogError(ex)
Finally
' Allow control to visually update
Me.ResumeLayout()
End Try
End Sub
--- CODE END ---
This is what FillList() looks like:
--- CODE START ---
Friend Sub FillList(ByVal nameValueCollection As Collection)
Dim item As NameValue
' If we are to include a blank item in the list, then do so.
If _IncludeBlankItem Then Me.Items.Insert(0, String.Empty)
' If the collection we were given has something in it...
If Not IsNothing(nameValueCollection) Then
' Loop thru the collection
For Each item In nameValueCollection
' Add the item to the list
Me.Items.Add(item)
Next
End If
End Sub
--- CODE END ---
In the method above is the only line of code that adds a blank item to the
combobox. I'm guessing, maybe, when VB creates the objects on the form it
calls the constructor, which calls Populate(), which calls FillList()? And
maybe the GetList() is returning Nothing because the information it
retrieves is not available at that time, so it just adds the blank line and
that's it? I don't think this is the case because I've commented out that
line and it still does it. I've event commented out the call to the
Populate() method in the combobox that inherits EnumCombobox and I still get
extra blank items added after every compile.
There is also nothing special in the constructor for EnumCombobox. I
haven't modified it at all. I am simply at a loss here.
- Don