vb.net InitializeComponent() 未声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18446937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:47:20  来源:igfitidea点击:

vb.net InitializeComponent() is not declared

vb.netinitializecomponent

提问by MaBi

I want to use InitializeComponent() when I create a custom control (to be sure everthing is initialized before I use it), but the compiler says it's not declared. But my Designer.vb contains a sub:

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

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

with all new instances I created.

使用我创建的所有新实例。

Why can't I call that?

为什么我不能叫它?

edit this is how I call InitizialeComponent:

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

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

回答by MACN

InitializeComponent() is private, and can only be called from inside that class. It is called by default by the usercontrol constructor, like this:

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

Public Sub New()

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

    ' Add any initialization after the InitializeComponent() call.

End Sub

Note that you will only have to call InitializeComponent() yourself if you overload the constructor. Default constructor does it by itself.

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

回答by Alex

You should not depend on your Designer.vb's InitializeComponent. Rather, create a new constructor in your Form1which will call InitializeComponent()

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

For example:

例如:

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

Now whenever we use the following code:

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

Dim NewFrm1 As New Form1

The Newconstructor in Form1 will be called.

NewForm1 中的构造函数将被调用。