在应用程序启动时以编程方式更改启动表单?

Jer*_*ild 2 vb.net startup winforms

我可以在VB.Net中以编程方式更改应用程序启动时的启动表单吗?

Mic*_*ues 7

你当然可以!

在"项目属性"中,将"启动对象"设置为"Sub Main",并确保应用程序中的某个位置有Public Sub Main方法.一个单独的启动类可能是一个好主意:

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
Run Code Online (Sandbox Code Playgroud)

End Class