简体   繁体   English

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

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

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 Application并且我得到一个带有static 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? 在决定在VB.Net中显示哪种表单之前,是否有等效的机制来运行自己的代码? I'm assuming the same code exists but is auto-generated and hidden somewhere? 我假设存在相同的代码,但是会自动生成并隐藏在某个地方? If so, where? 如果是这样,在哪里?

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 希望能有所帮助

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. 如果您的应用程序只有一种形式 ,则可以简单地开始输入Sub New(),Visual Studio将自动生成该方法存根,该方法存根首先执行。 No project setting changes required. 无需更改项目设置。

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

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

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