如何在VB.Net winforms应用程序中找到main()入口点?

时间:2021-07-13 00:02:09

When I create a WinForms app in C#, the output type is Windows Application and I get a program.cs with a static void Main() which I can use to handle command-line parameters, etc...

当我在C#中创建WinForms应用程序时,输出类型是Windows应用程序,我得到一个带有静态void Main()的program.cs,我可以使用它来处理命令行参数等...

However, when I create an equivalent project for VB, the application type is Windows Forms Application and I'm forced to pick a startup form.

但是,当我为VB创建一个等效项目时,应用程序类型是Windows Forms Application,我不得不选择一个启动表单。

Is there an equivalent mechanism to run my own code before I decide which form to display in VB.Net? I'm assuming the same code exists but is auto-generated and hidden somewhere? If so, where?

在决定在VB.Net中显示哪个表单之前,是否有相同的机制来运行我自己的代码?我假设存在相同的代码但是自动生成并隐藏在某处?如果是这样,在哪里?

2 个解决方案

#1


39  

In VB you'll need to create your sub main by hand as it were so what I generally do is create a new Module named Program.

在VB中,您需要手动创建子主体,因此我通常会创建一个名为Program的新模块。

Within that as a very basic setup you'll need to add the following code.

在其中作为一个非常基本的设置,您需要添加以下代码。

Public Sub Main()

    Application.EnableVisualStyles()
    Application.SetCompatibleTextRenderingDefault(False)
    Application.Run(New Form1)

End Sub

Once you've done that go to the application tab of the project's settings and uncheck the 'Enable Application framework' setting and then from the drop down under Startup object select your new Sub Main procedure.

完成后,转到项目设置的应用程序选项卡,取消选中“启用应用程序框架”设置,然后从“启动对象”下拉列表中选择新的“主要”主程序。

You can then put any start-up code that you want to run before your program opens its main form before the Application.Run line.

然后,您可以在程序在Application.Run行之前打开其主窗体之前放置要运行的任何启动代码。

Hope that helps

希望有所帮助

#2


1  

If your application has only one form you may simply start typing Sub New() and Visual Studio will autogenerate this method stub which executes first. No project setting changes required.

如果您的应用程序只有一个表单,您可以简单地开始键入Sub New(),Visual Studio将自动生成此方法存根,该存根首先执行。无需更改项目设置。

Public Sub New()
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

    'Add any initialization after the InitializeComponent() call
    '==> Put your pre-execution steps here.
End Sub

#1


39  

In VB you'll need to create your sub main by hand as it were so what I generally do is create a new Module named Program.

在VB中,您需要手动创建子主体,因此我通常会创建一个名为Program的新模块。

Within that as a very basic setup you'll need to add the following code.

在其中作为一个非常基本的设置,您需要添加以下代码。

Public Sub Main()

    Application.EnableVisualStyles()
    Application.SetCompatibleTextRenderingDefault(False)
    Application.Run(New Form1)

End Sub

Once you've done that go to the application tab of the project's settings and uncheck the 'Enable Application framework' setting and then from the drop down under Startup object select your new Sub Main procedure.

完成后,转到项目设置的应用程序选项卡,取消选中“启用应用程序框架”设置,然后从“启动对象”下拉列表中选择新的“主要”主程序。

You can then put any start-up code that you want to run before your program opens its main form before the Application.Run line.

然后,您可以在程序在Application.Run行之前打开其主窗体之前放置要运行的任何启动代码。

Hope that helps

希望有所帮助

#2


1  

If your application has only one form you may simply start typing Sub New() and Visual Studio will autogenerate this method stub which executes first. No project setting changes required.

如果您的应用程序只有一个表单,您可以简单地开始键入Sub New(),Visual Studio将自动生成此方法存根,该存根首先执行。无需更改项目设置。

Public Sub New()
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

    'Add any initialization after the InitializeComponent() call
    '==> Put your pre-execution steps here.
End Sub