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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14306903/
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-03 16:49:17  来源:igfitidea点击:

How to find the main() entry point in a VB.Net winforms app?

.netvb.netwinforms

提问by Basic

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

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

However, when I create an equivalent project for VB, the application type is Windows Forms Applicationand 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 中显示哪种表单之前,是否有等效的机制来运行我自己的代码?我假设存在相同的代码但自动生成并隐藏在某处?如果有,在哪里?

回答by Dom Sinclair

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.

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

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

希望有帮助

回答by Jeff

If your application has only one formyou 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