共用方式為


My.Application.Startup 事件

更新:2007 年 11 月

發生於應用程式啟動的時候。

 ' Usage Public Sub Me_Startup( _    ByVal sender As Object, _    ByVal e As StartupEventArgs _ ) Handles Me.Startup End Sub ' Declaration Public Event Startup( _    ByVal sender As Object, _    ByVal e As StartupEventArgs _ )

參數

  • sender
    引發事件的 Object。

  • e
    StartupEventArgs 物件,包含應用程式的命令列引數。

備註

一般 (非單一執行個體) 應用程式會在每次啟動時引發 Startup 事件。唯有單一執行個體的應用程式尚未在使用中時,此應用程式才會在啟動時引發 Startup 事件。否則,它會引發 StartupNextInstance 事件。如需詳細資訊,請參閱 My.Application.StartupNextInstance 事件HOW TO:指定應用程式的執行個體行為 (Visual Basic)

這個事件是 Visual Basic 應用程式模型的一部分。如需詳細資訊,請參閱 Visual Basic 應用程式模型概觀

您可以使用 e 參數的 Cancel 屬性,控制應用程式啟動表單的載入。Cancel 屬性已設為 True 時,啟動表單不會啟動。在該情況下,您的程式碼應該呼叫替代啟動程式碼路徑。如需範例,請參閱 HOW TO:啟用 Window Form 應用程式的批次模式

您可以使用 e 參數的 CommandLine 屬性或 My.Application.CommandLineArgs 屬性,存取應用程式的命令列引數。

Startup 事件處理常式的程式碼儲存在 ApplicationEvents.vb 檔中,檔案預設為隱藏檔。

若要存取應用程式事件的程式碼編輯器視窗

  1. 在 [方案總管] 中選取了專案之後,請在 [專案] 功能表,按一下 [屬性]。

  2. 按一下 [應用程式] 索引標籤。

  3. 按一下 [檢視應用程式事件] 按鈕,開啟 [程式碼編輯器]。

如需詳細資訊,請參閱 HOW TO:處理應用程式事件 (Visual Basic)

工作

下列表格列出包含 My.Application.Startup 事件的工作範例。

若要

請參閱

請使用 Visual Basic 應用程式模型所提供的事件,執行程式碼

HOW TO:當應用程式啟動或結束時執行程式碼

檢查應用程式是否以字串 /batch 做為引數而啟動

HOW TO:啟用 Window Form 應用程式的批次模式

範例

這個範例使用 My.Application.SplashScreen 屬性和 My.Application.Startup 事件,以應用程式啟動時的狀態資訊更新啟動顯示畫面。

Private Sub MyApplication_Startup( _
    ByVal sender As Object, _
    ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs _
) Handles Me.Startup
    ' Get the splash screen.
    Dim splash As SplashScreen1 = CType(My.Application.SplashScreen, SplashScreen1)
    ' Display current status information.
    splash.Status = "Current user: " & My.User.Name
End Sub

這個範例要求專案必須有名為 SplashScreen1 的啟動顯示畫面。啟動顯示畫面必須有名為 Status 的屬性,此屬性會更新使用者介面。

您必須在應用程式事件的 [程式碼編輯器] 視窗中輸入程式碼。若要存取這個視窗,請依照此主題中註解章節的指示進行。如需詳細資訊,請參閱 HOW TO:處理應用程式事件 (Visual Basic)

需求

命名空間 (Namespace):Microsoft.VisualBasic.ApplicationServices

類別︰WindowsFormsApplicationBase

組件 (Assembly):Visual Basic Runtime Library (在 Microsoft.VisualBasic.dll 中)

依專案類型的可用性

專案類型

是否可用

Windows 應用程式

類別庫

主控台應用程式

Windows 控制項程式庫

Web 控制項程式庫

Windows 服務

網站

使用權限

下列使用權限可能為必要:

使用權限

描述

SecurityPermission

控制為這個事件加入事件處理常式的能力。關聯的列舉型別:SecurityPermissionFlag.ControlAppDomain

如需詳細資訊,請參閱程式碼存取安全性要求使用權限

請參閱

工作

HOW TO:啟用 Window Form 應用程式的批次模式

HOW TO:處理應用程式事件 (Visual Basic)

HOW TO:指定應用程式的執行個體行為 (Visual Basic)

概念

Visual Basic 應用程式模型概觀

參考

My.Application 物件

My.Application.StartupNextInstance 事件

My.Application.CommandLineArgs 屬性

Microsoft.VisualBasic.ApplicationServices.StartupEventArgs

WindowsFormsApplicationBase.Startup