繁体   English   中英

vb.net InitializeComponent()未声明

[英]vb.net InitializeComponent() is not declared

我想在创建自定义控件时使用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

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

Public Sub New()

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

    ' Add any initialization after the InitializeComponent() call.

End Sub

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

您不应该依赖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构造函数将被调用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM