次の方法で共有


方法 : Windows フォーム アプリケーションのバッチ モードを有効にする

更新 : 2007 年 11 月

この例では、My.Application.Startup イベントを使用して、アプリケーションの起動時に文字列 /batch が引数に指定されたかどうかをチェックします。

Windows フォーム アプリケーションのバッチモードを有効にするには

  1. ソリューション エクスプローラでプロジェクトを選択します。[プロジェクト] メニューの [プロパティ] をクリックします。

  2. [アプリケーション] タブの [アプリケーション イベントの表示] をクリックして、コード エディタを開きます。

  3. My.Application.Startup イベントを処理するメソッドを作成します。詳細については、「方法 : アプリケーション イベントを処理する (Visual Basic)」を参照してください。

    Private Sub MyApplication_Startup( _
        ByVal sender As Object, _
        ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs _
    ) Handles Me.Startup
    
    End Sub
    
  4. アプリケーションの各コマンド ライン引数に対して反復処理を行い、いずれかの引数が /batch の場合に、e オブジェクトの Cancel プロパティを True に設定します。

    Cancel プロパティが True に設定されている場合、スタートアップ フォームは開始されません。

    For Each s As String In My.Application.CommandLineArgs
        If s.ToLower = "/batch" Then
            ' Stop the start form from loading.
            e.Cancel = True
        End If
    Next
    
  5. e オブジェクトの Cancel プロパティが True に設定されている場合、ウィンドウなしの操作用のメイン ルーチンを呼び出します。

    If e.Cancel Then
        ' Call the main routine for windowless operation.
        Dim c As New BatchApplication
        c.Main()
    End If
    

使用例

Private Sub MyApplication_Startup( _
    ByVal sender As Object, _
    ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs _
) Handles Me.Startup
    For Each s As String In My.Application.CommandLineArgs
        If s.ToLower = "/batch" Then
            ' Stop the start form from loading.
            e.Cancel = True
        End If
    Next
    If e.Cancel Then
        ' Call the main routine for windowless operation.
        Dim c As New BatchApplication
        c.Main()
    End If
End Sub
Class BatchApplication
    Sub Main()
        ' Insert code to run without a graphical user interface.
    End Sub
End Class

参照

処理手順

方法 : コマンド ライン引数にアクセスする (Visual Basic)

概念

Visual Basic アプリケーション モデルの概要

参照

My.Application オブジェクト

My.Application.CommandLineArgs プロパティ