Search code examples
vb.netvisual-studio-2013vb6-migration

My main form is closing immediately


I have the situation that when I run my app my main form is closing immediately. I don’t know why, here is the code I’m using:

Public Sub Main()
    ...
    form.Visible = True
    form.Show()
    ...
End Sub

This code is from an app from vb6.0 upgraded to vb2013, obviously there is a lot of code in this app for to make a new app, please the problem is when I execute the program and the main form initialize it closes immediately.

Any help would be appreciated.


Solution

  • If you set the Startup Object of your application to a Sub Main and inside this Main you want to launch your form then your Main should be something like this

    Public Sub Main()
        Application.EnableVisualStyles()
        Application.SetCompatibleTextRenderingDefault(False)
        Application.Run(New Form1)
    End Sub
    

    Actually your code terminates immediately because you show the form non modally (form.Show()) and this means that the call exits immediately. Of course this also means that you program terminates because you exit from the Sub Main.