Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically change the startup form on application launch?

Can I programmatically change the startup form on application launch in VB.Net?

like image 245
Jeremy Child Avatar asked May 18 '26 09:05

Jeremy Child


1 Answers

Sure you can!

In your Project Properties, set Startup Object to Sub Main, and make sure that there's a Public Sub Main method somewhere in your application. A separate startup class may be a good idea:

Public Class myStartupClass

''' <summary>
''' This is the method that will be run when the application loads, 
''' because Project Properties, Startup Object is set to SubMain
''' </summary>
''' <remarks>
''' </remarks>
''' --------------------------------------------------------------------------------
Public Shared Sub Main()

    'The form that we will end up showing
    Dim formToShow As System.Windows.Forms.Form = Nothing

    'The determiner as to which form to show
    Dim myMood As String = "Happy"

    'Choose the appropriate form
    Select Case myMood
        Case "Happy"
            formToShow = New Form1
        Case Else
            formToShow = New Form2
    End Select

    'Show the form, and keep it open until it's explicitly closed.
    formToShow.ShowDialog()

End Sub

End Class

like image 90
Michael Rodrigues Avatar answered May 19 '26 22:05

Michael Rodrigues