Programmatically change the startup form on application launch?

12,527

Solution 1

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

Solution 2

In a "Windows Forms Application", created under VB 2010 Express, you can do the following in ApplicationEvents.vb:

Partial Friend Class MyApplication

    Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup

       'Select the form of your choice
       My.Application.MainForm = Any_Form_You_like

    End Sub

End Class
Share:
12,527
Jeremy Child
Author by

Jeremy Child

Updated on July 11, 2022

Comments

  • Jeremy Child
    Jeremy Child almost 2 years

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

  • Oak_3260548
    Oak_3260548 over 4 years
    Be aware, for this option you must switch a Windows Form Application (if you have it, which would be in most cases in this scenario) into Console Application. That has a lot of consequences. This might be difficult and unfeasible action with existing application.
  • Oak_3260548
    Oak_3260548 over 4 years
    The same I wrote to the other answer of this type. Be aware, for this option you must switch a Windows Form Application (if you have it, which would be in most cases in this scenario) into Console Application. That has a lot of consequences. This might be difficult and unfeasible action with existing application.
  • DarrenMB
    DarrenMB about 4 years
    It is used this way for a console app or a forms app set to sub main. I have yet to find any consequences with this approach so far, the documentation basically indicates "Application.Run(form)" is how a forms based app is created and they even reference this to be the correct way to do it... You should use the Run(Form) overload to start an application with a main form, so that the application terminates when the main form is closed link
  • Oak_3260548
    Oak_3260548 about 4 years
    It's true, that I didn't test it with this particular code. I played with settings around and set it using GUI, including Sub Main() variant. VS threw an error and explicitly told me I am running a Windows Forms Application and that I have to switch to Console Application. I also run into another issues with suppresing the main form, so I ended up on just hiding it and showing another form. It might be sensible just in my scenario, though.