5

Step 1: Create inhered control class

Public Class Test_Control
    Inherits ListBox

    Public Sub New()
        Items.Add("test")
    End Sub
End Class

Step 2: Drag class to form in the designer

enter image description here

Step 3: Run the project

Result:

enter image description here

Why is this happening?! I am completely stumped here.. I have googled and googled and I cannot find any solution or answer to this.

This is causing some major issues for me. I am simply trying to add an initial "Select one..." option to every newly created Combobox. Same thing happens with every inherited control class, regardless of control type (textbox/combobox/listbox/etc).

Same thing happens if I use a message box within New(). Two message boxes appear as soon as I run my application.

enter image description here

cowsay
  • 1,282
  • 1
  • 15
  • 36

2 Answers2

5

You need to tell the designer to not serialize the items collection:

Public Class Test_Control
  Inherits ListBox

  Public Sub New()
    Items.Add("test")
  End Sub

  <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
  Public Shadows ReadOnly Property Items As ListBox.ObjectCollection
    Get
      Return MyBase.Items
    End Get
  End Property
End Class

As far as the two message boxes go, MessageBoxes are just not a good debugging tool. You are probably getting the WinForms designer calling new while the runtime calling new, too (or something like that).

LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • A guy learns something new every single day. I didn't know that was necessary to keep it from generating the designer code. – Mike Perrenoud Jul 25 '13 at 19:06
  • This is a great answer thank you.. and it does fix the issue as far as list items go. It seems when I run the project (F5), the constructor fires during build process. Then it fires again the moment the application starts.. hence the two message boxes. It does indeed seem that these are two separate issues.. one being the designer serialization and the other being the constructor firing during build. When I run the application manually from my debug folder, the dual message boxes don't appear. – cowsay Jul 25 '13 at 19:16
  • 1
    @MichaelPerrenoud I don't think the control knows it's in a design environment until after the constructor is run. – LarsTech Jul 25 '13 at 19:16
  • @LarsTech, that makes sense. What an interesting conundrum, because that's generally when you'd want to know, but honestly the clear workaround for that is the `SerializationVisibility` attribute. – Mike Perrenoud Jul 25 '13 at 19:18
  • 1
    @MichaelPerrenoud This post seems to have an answer (and solution) for that http://stackoverflow.com/questions/1166226/detecting-design-mode-from-a-controls-constructor -- thank you for your input – cowsay Jul 25 '13 at 19:19
2

The first test is from the designer and you are adding a second one in the constructor.

Either remove the test from the designer or clear the items in the constructor before adding, like this:

Items.Clear()
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
  • The one in the designer appears automatically as a result of the code in the constructor. The item appears as soon as I place the control on the form from within the designer. But it doesn't just happen to listbox items... it also makes two message boxes appear if I use MsgBox in the constructor. See the edit to my main post. – cowsay Jul 25 '13 at 18:53
  • What is in your `Form1.vb` file? – Karl Anderson Jul 25 '13 at 18:57
  • Absolutely nothing.. no code at all. The steps listed in my original post are exactly what I've done to reproduce this.. no more, no less. – cowsay Jul 25 '13 at 19:09