1

I have a custom control MyControl. Has a parameterless constructor (Sub New() in VB).

I place that control in a WinForm.

No problems.

Now, want a parameter in that constructor. Sub New(flag as Boolean)

How should I deal with VS designer in that case, in order to be able to open that form in Designer?

I did in myForm the following

  Public Sub New()
    MyBase.New()
    Me.MyControl_1 = New MyControl(True)
    Me.InitializeComponent()

but the Designer says:

The variable 'MyControl_1' is either undeclared or was never assigned.

serhio
  • 28,010
  • 62
  • 221
  • 374

2 Answers2

2

The designer requires a parameterless constructor for visual components. Even adding an overloaded constructor to a user control will break the designer. See this question for more information.

Community
  • 1
  • 1
AJ.
  • 16,368
  • 20
  • 95
  • 150
  • 1
    yeah. I understand. But I can't use properties, because I have a control "mode" that can't be changed after its creation. (like readonly one). So I can't and wouldn't use properties. I need this "mode" be set only once, as earlier as possible. So I search a workaround. – serhio Feb 15 '11 at 14:26
  • 1
    an overloaded constructor does NOT break the designer, as long as there is also a parameterless constructor – yoel halb Jan 01 '14 at 19:01
1

Modify to this

Public Sub New()
    MyBase.New()
    Me.InitializeComponent()
    Me.MyControl_1 = New MyControl(True)

Let designer initialize the cotrol before you use it. Problem is not with your control, problem is you are not using at right place.

hungryMind
  • 6,931
  • 4
  • 29
  • 45