18

What is the equivalent of Program.cs in VB.NET WinForms application?

I need to move my .CS app to VB.NET.

As I do not have forms in my WinForms project in Program.cs I do the following:

Application.Run(new MyForm(parm1, parm2);

How to move this code in VB.NET?

Thanks

3
  • 6
    Can't understand your anger. I search before and no answer Google gave me was satisfying. @SteveDog cleared all my doubts and helped me make my app running properly.
    – MaiOM
    May 10, 2012 at 14:05
  • 2
    One way to avoid complaints like @Ramhound's is to show what you found when searching, and explain why it was not satisfying. Either way, I'm glad you asked, because the answers helped me.
    – Don Kirkby
    Mar 14, 2013 at 18:33
  • 2
    I agree with Don Kirkby, this question appears on the top when I googled 'vb.net program.cs'.
    – ABH
    Nov 5, 2013 at 7:43

3 Answers 3

30

By default, VB.NET applications use the mysterious Windows Application Framework which means that the build process adds an entry point for you which automatically runs whichever form you use as the main form. You can access these settings via Project Properties > Application > Windows application framework properties.

If you don't want to use the framework, un-check the Enable application framework check box and then select that you want to use Sub Main as your Startup object. Then you can create a module with a Public Sub Main method which will be the entry point to the application, just like in Program.cs in C#.

0
9

It doesn't matter whether it's Program.cs or YouNameIt.cs.

The default entry point is Main().

This code should work, unless you have another entry point (Can be specified in project settings).

Shared Sub Main()     
   ' Starts the application.
     Application.Run(New Form1())
End Sub
1
  • Does this need to be a Module? And if so, is the Shared keyword redundant?
    – Jeff B
    Apr 2, 2020 at 21:58
5

As Steven's answer says, by default you don't need anything like Program.cs in a VB.NET application. If you want to replace the standard launch code, however, you can add a new module and select it as the startup object.

First, right click on the project and choose Add: Module.... You can name the module anything you like, but I named it Program.vb to be consistent with the C# default. Paste in the following code:

Module Program
    Public Sub Main()
        Application.Run(New Form1()) ''//Use your main form here
    End Sub
End Module

Next, right click on the project and choose Properties. On the Application tab, uncheck "Enable application framework". Then change the startup object to Program. There's another option called Sub Main that I think will run the Main() subroutine wherever it is.

Now that you've replaced the standard launch code, you can add other things to your Main() subroutine. I noticed one other thing on MSDN (How to: Change the Startup Object for an Application):

When using a custom Sub Main procedure as the Startup object, code in the application events (Startup, Shutdown, StartupNextInstance, and UnhandledException) is not executed.

1
  • Thanks for the extra clarification. Moving from C# to VB .NET is bound to raise questions like this one. Jan 10, 2022 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.