次の方法で共有


方法 : メッセージ ボックスを表示する

MessageBox は、ユーザーにアプリケーション関連の情報を表示するための、定義済みダイアログ ボックスです。 メッセージ ボックスは、ユーザーに情報の入力を要求するときにも使用できます。

メッセージ ボックスにユーザーへの情報を表示するには

  1. メッセージ ボックスを表示するコードを追加する場所に移動します。

  2. Show メソッドを使用してコードを追加します。

    MessageBox クラスの Show メソッドを呼び出してユーザーに情報を表示する方法のコード例を次に示します。 Show メソッドの呼び出しでは、style パラメーターを使用して、メッセージ ボックスに表示するアイコンの種類を指定します。このとき、表示するメッセージ ボックスの種類に適したアイコンを指定します。

    Public Sub PerformCalculations()
       ' Code is entered here that performs a calculation.
       ' Display a message box informing the user that the calculations 
       ' are complete.
          MessageBox.Show("The calculations are complete", "My Application", _
               MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk)
    End Sub
    
    public void PerformCalculations() 
    {
       // Code is entered here that performs a calculation
       // Display a message box informing the user that the calculations 
       // are complete
       MessageBox.Show ("The calculations are complete", "My Application", 
    MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
    }
    
    public:
       void PerformCalculations()
       {
          // Code is entered here that performs a calculation
          // Display a message box informing the user that the calculations 
          // are complete
          MessageBox::Show("The calculations are complete",
             "My Application", MessageBoxButtons::OKCancel,
             MessageBoxIcon::Asterisk);
       }
    

    メッセージ ボックスでは、入力を受け取ることもできます。 MessageBox クラスの Show メソッドでは、ユーザーの選択を確認するために使用する値を返します。 この値は、整数の形式で格納するか、または if ステートメントを使用してメッセージ ボックスを表示するときに返された値と比較します。 ユーザーに情報の入力を要求するボタンを表示するには、Show メソッドの style パラメーターを設定します。

情報を要求するメッセージ ボックスを表示するには

  1. クラスのコード エディターを表示し、メッセージ ボックスを表示させるコードを追加する場所に移動します。

  2. MessageBox クラスの Show メソッドを使用してメッセージ ボックスを表示するコードを追加します。

    MessageBox メソッドを呼び出してユーザーの情報を取得し、選択された値を確認する方法の例は、次のとおりです。

    Public Sub ExitApplication()
       ' Display a message box asking users if they 
       ' want to exit the application.
       If MessageBox.Show ("Do you want to exit?", "My Application", _
             MessageBoxButtons.YesNo, MessageBoxIcon.Question) _
             = DialogResult.Yes Then
          Application.Exit
       End If
    End Sub
    
    public void ExitApplication()
    {
       // Display a message box asking users if they
       // want to exit the application.
       if (MessageBox.Show ("Do you want to exit?", "My Application",
             MessageBoxButtons.YesNo, MessageBoxIcon.Question)
             == DialogResult.Yes) 
       {
          Application.Exit();
       }
    }
    
    public:
       void ExitApplication()
       {
          // Display a message box asking users if they
          // want to exit the application.
          if (MessageBox::Show("Do you want to exit?",
             "My Application", MessageBoxButtons::YesNo,
             MessageBoxIcon::Question) == DialogResult::Yes)
          {
             Application::Exit();
          }
       }
    
    Visual Basic メモVisual Basic メモ

    Visual Basic では、ユーザーに表示するメッセージ ボックスを MsgBox() で作成することが引き続きサポートされていますが、上に示した新しい構文の MessageBox.Show() を使用することをお勧めします。 したがって、前のコード例について、Visual Basic では次のようなコードも使用できます。

    Public Sub ExitApplication()
       If MsgBox("Do you want to exit?", MsgBoxStyle.Exclamation, _
    "My Application") = MsgBoxResult.Yes Then
         Application.Exit()
       End If
    End Sub
    

    MsgBox() の詳細については、MsgBox 関数のトピックを参照してください。

参照

処理手順

方法 : デザイン時にダイアログ ボックスを作成する

参照

MessageBox

MsgBox Result Constants for Visual Basic 6.0 Users

MsgBox Style Constants for Visual Basic 6.0 Users

Form.DialogResult

その他の技術情報

Windows フォームのダイアログ ボックス

新しい Windows フォームの作成