方法 : メッセージ ボックスを表示する
MessageBox は、ユーザーにアプリケーション関連の情報を表示するための、定義済みダイアログ ボックスです。メッセージ ボックスは、ユーザーに情報の入力を要求するときにも使用できます。
メッセージ ボックスにユーザーへの情報を表示するには
メッセージ ボックスを表示するコードを追加する場所に移動します。
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 パラメータを設定します。
情報を要求するメッセージ ボックスを表示するには
クラスのコード エディタを表示し、メッセージ ボックスを表示させるコードを追加する場所に移動します。
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 では、ユーザーに表示するメッセージ ボックスを
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 Class
MsgBox Result定数 (Visual Basic 6.0 ユーザー向け)
MsgBox Style 定数 (Visual Basic 6.0 ユーザー向け)
Form.DialogResult Property