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

时间:2010-02-16 09:45:20

标签: vb.net winforms startup

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

4 个答案:

答案 0 :(得分: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

End Class

答案 1 :(得分:4)

在VB 2010 Express下创建的“Windows窗体应用程序”中,您可以在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

答案 2 :(得分:0)

这是我使用的方法,它有效地启动表格,就像正常的应用程序入口点一样。

Public Sub Main()
    Dim value As String = Trim(Environment.CommandLine)
    Dim f As Form
    Select Case value
        Case "a"
            f = New frmTextEdit
        Case "b"
            f = New frmListDialog
        Case "c"
            f = New frmSuggestion
        Case Else
            Throw New Exception("Unsupported startup form option")
    End Select
    Application.Run(f)
End Sub

答案 3 :(得分:0)

正确的方法是更改​​项目的MainForm。

  1. 打开 ApplicationsEvents.vb

  2. 在 ApplicationStart.vb 中使用:

        Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
        Dim formToShow As System.Windows.Forms.Form = Nothing
        Dim AM_PM as String = "AM"
        Select Case 
            Case "AM"
                Me.MainForm = frmBefore
            Case "PM"
                Me.MainForm = frmAfter
        End Select
        MainForm.Show()
    End Sub
    

原来如此。