1

我想在创建自定义控件时使用 InitializeComponent() (以确保在使用它之前初始化所有内容),但编译器说它没有声明。但我的 Designer.vb 包含一个子:

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

我创建的所有新实例。

为什么我不能这么叫?

编辑这就是我调用 InitizialeComponent 的方式:

Public Class CustomControlsTextBox : Inherits TextBox
Public Sub New()
    MyBase.New()
    InitizialeComponent() 'this function is not declared
End Sub
End Class
4

2 回答 2

2

InitializeComponent() 是私有的,只能从该类内部调用。默认情况下,它由 usercontrol 构造函数调用,如下所示:

Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

End Sub

请注意,如果您重载构造函数,您只需要自己调用 InitializeComponent()。默认构造函数自己完成。

于 2013-08-26T15:07:52.677 回答
0

您不应依赖 Designer.vb 的 InitializeComponent。相反,在您的中创建一个新的构造函数Form1,它将调用 InitializeComponent()

例如:

Public Class Form1

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        'Type all your code here! This code will be executed before the "FormLoad" if you call your new form
    End Sub
End Class

现在每当我们使用以下代码时:

Dim NewFrm1 As New Form1

将调用 Form1 中的New构造函数。

于 2013-08-26T15:00:44.597 回答