Rose
Rose

Reputation: 641

VB.NET: How to control the Form that is displayed when the program starts from Sub Main

VB.NET 2012 My Startup Object is set to (Sub Main). The app needs to collect a few different sets of data before the primary form is loaded

This article http://msdn.microsoft.com/en-us/library/ms235406(v=vs.110).aspx mentions

In Main, you can determine which form is to be loaded first when the program starts

But it never explains how to show the form

If I use ShowDialog the application terminates when mainView’s Visible property is set to False or when mainView is Hidden

Module Module1
    Public mainView As New Form1
    Public Sub Main()
        ' initialization code
        mainView.ShowDialog() ' this works until I need to hide mainView, ShowDialog returns and the app terminates
    End Sub
End Module

If I use Show the application immediately falls out of Sub Main and terminates

Module Module1
    Public mainView As New Form1
    Public Sub Main()
        ' initialization code
        mainView.Show() ' this doesn't work at all, the app terminates as soon as Main is executed
    End Sub
End Module

What is the best approach to achieve these requirements?

Upvotes: 1

Views: 1041

Answers (1)

Rose
Rose

Reputation: 641

This seems to work perfectly. I had read about the messaging loop but it didn’t seem to work until I tried it like below, thanks LarsTech

Module Module1
    Public mainView As Form1
    Public Sub Main()
        ' initialization code
        ''...
        Application.EnableVisualStyles()
        Application.SetCompatibleTextRenderingDefault(False)
        mainView = New Form1
        Application.Run(mainView) ' I can reference 'mainView' from anywhere in my app, toggle its Visible  property etc.
    End Sub
End Module

Upvotes: 2

Related Questions