开发者

VS Designer and constructors with parameters

开发者 https://www.devze.com 2023-02-10 08:56 出处:网络
I have a custom control MyControl. Has a parameterless constructor (Sub New() in VB). I place that control in a WinForm.

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 foll开发者_运维技巧owing

  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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消