次の方法で共有


Application.Run メソッド (Form)

現在のスレッドで標準のアプリケーション メッセージ ループの実行を開始し、指定したフォームを表示します。

Overloads Public Shared Sub Run( _
   ByVal mainForm As Form _)
[C#]
public static void Run(FormmainForm);
[C++]
public: static void Run(Form* mainForm);
[JScript]
public static function Run(
   mainForm : Form);

パラメータ

  • mainForm
    表示するフォームを表す Form

例外

例外の種類 条件
InvalidOperationException メイン メッセージ ループが既に現在のスレッドで実行中です。

解説

通常、アプリケーションの main 関数でこのメソッドを呼び出し、アプリケーションのメイン ウィンドウに渡します。

このメソッドは、 Closed イベントの mainForm パラメータにイベント ハンドラを追加します。イベント ハンドラは、 ExitThread を呼び出して、アプリケーションをクリーンアップします。

メモ   このメソッドが返される前に、 Form クラスの Dispose メソッドが呼び出されます。

使用例

[Visual Basic, C#, C++] フォームのリスト ボックスに番号を一覧表示する例を次に示します。 button1 をクリックするたびに、リストに新しい番号が追加されます。

[Visual Basic, C#, C++] Main メソッドは、 Run を呼び出してアプリケーションを開始し、フォーム、 listBox1 、および button1 を作成します。 button1 をクリックすると、 button1_Click メソッドによってリスト ボックスに 1 から 3 の番号が追加され、 MessageBox が表示されます。 MessageBox の [ いいえ] をクリックすると、 button1_Click メソッドによってリストに新しい番号が追加されます。[ はい] をクリックすると、アプリケーションでは Exit を呼び出してキューに残っているメッセージをすべて処理し、終了します。

[Visual Basic, C#, C++] この例は、 listBox1button1 が既に作成され、フォームに配置されていることを前提としています。

 
<STAThread()> _
Shared Sub Main()     
   ' Starts the application.
   Application.Run(New Form1())
End Sub

Sub button1_Click(sender As object, e As System.EventArgs)
   ' Populates a list box with three numbers.
   Dim i As Integer = 3
   Dim j As Integer
   For j = 1 To i - 1
      listBox1.Items.Add(j)
   Next

   ' Checks to see whether the user wants to exit the application.
   ' If not, adds another number to the list box.
   While (MessageBox.Show("Exit application?", "", MessageBoxButtons.YesNo) = _ 
      DialogResult.No)
      ' Increments the counter and adds the number to the list box.
      i = i + 1
      listBox1.Items.Add(i)
   End While

   ' The user wants to exit the application. Close everything down.
   Application.Exit()
End Sub


[C#] 
public static void Main(string[] args) {
    // Starts the application.
    Application.Run(new Form1());
 }
 
 protected void button1_Click(object sender, System.EventArgs e) {
    // Populates a list box with three numbers.
    int i = 3;
    for(int j=1; j<=i; j++) {
       listBox1.Items.Add(j);
    }
 
    /* Determines whether the user wants to exit the application.
     * If not, adds another number to the list box. */
    while (MessageBox.Show("Exit application?", "", MessageBoxButtons.YesNo) == 
       DialogResult.No) {
       // Increments the counter ands add the number to the list box.
       i++;
       listBox1.Items.Add(i);
    }
 
    // The user wants to exit the application. Close everything down.
    Application.Exit();
 }


[C++] 
public:
   static void main() {
      // Starts the application.
      Application::Run(new Form1());
   }

protected:
   void button1_Click(Object* sender, System::EventArgs* e) {
      // Populates a list box with three numbers.
      int i = 3;
      for (int j=1; j<=i; j++) {
         listBox1->Items->Add(__box(j));
      }

      /* Determines whether the user wants to exit the application.
      * If not, adds another number to the list box. */
      while (MessageBox::Show(S"Exit application?", S"", MessageBoxButtons::YesNo) ==
         DialogResult::No) {
            // Increments the counter ands add the number to the list box.
            i++;
            listBox1->Items->Add(__box(i));
         }

         // The user wants to exit the application. Close everything down.
         Application::Exit();
   }

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

Application クラス | Application メンバ | System.Windows.Forms 名前空間 | Application.Run オーバーロードの一覧 | Exit | ExitThread | DoEvents