[VC2005] 色々なメッセージボックスを表示する方法
◆概要
この資料は、Microsoft(R) Visual C# 2005で 色々なメッセージボックスを表示する方法について記述しています。
◆Contents
1.中止、再試行、無視
2.メッセージボックスのキャプションを右寄せにする
3.アイコンをExclamationにする
4.アイコンをAsteriskにする
5.アイコンをErrorにする
6.アイコンをWarningにする
7.アイコンなし、OKとキャンセルボタンにする
8.再試行とキャンセルボタンにする
9.「はい」ボタンと「いいえ」ボタンにする
10.「はい」ボタンと「いいえ」ボタン、「キャンセル」ボタンにする
11.「中止」ボタンと「再試行」ボタン、「無視」ボタンにする
メッセージボックスに、「中止、再試行、無視」 ボタンを表示する方法です。
private void button1_Click(object sender, EventArgs e) { MessageBox.Show(this,"メッセージの内容","Title", MessageBoxButtons.AbortRetryIgnore); } |
◆実行結果

▼ページトップへ
◆2.メッセージボックスのキャプションを右寄せにする
次のようにフォームにserverNameというテキストボックスとbutton1というコマンドボタンを貼り付けます。

button1をダブルクリックしてコードを次の通り書きます。
private void button1_Click(object sender, EventArgs e) { if (textBox1.Text.Length == 0) { //メッセージボックスを表示するための変数を初期化 string message = "名前が入力されていません。キャンセルしますか?"; string caption = "名前未入力"; MessageBoxButtons buttons = MessageBoxButtons.YesNo; DialogResult result; // メッセージボックスを表示する result = MessageBox.Show(this, message, caption, buttons, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign); if (result == DialogResult.Yes) { // 親フォームを閉じる this.Close(); } } } |
◆実行結果

キャプションが右側にあることに注意してください。
▼ページトップへ
◆3.アイコンをExclamationにする
private void button1_Click(object sender, EventArgs e) { MessageBox.Show(this, "メッセージの内容", "Title", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation); } |
◆実行結果

▼ページトップへ
◆4.アイコンをAsteriskにする
private void button1_Click(object sender, EventArgs e) { MessageBox.Show(this, "メッセージの内容", "Title", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Asterisk); } |
◆実行結果

▼ページトップへ
◆5.アイコンをErrorにする
private void button1_Click(object sender, EventArgs e) { MessageBox.Show(this, "メッセージの内容", "Title", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); } |
◆実行結果

アイコンをHandやStopにしても同じ結果になります。
▼ページトップへ◆6.アイコンをWarningにする
private void button1_Click(object sender, EventArgs e) { MessageBox.Show(this, "メッセージの内容", "Title", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning); } |
◆実行結果

▼ページトップへ
◆7.アイコンなし、OKとキャンセルボタンにする
private void button1_Click(object sender, EventArgs e) { MessageBox.Show(this, "メッセージの内容", "Title", System.Windows.Forms.MessageBoxButtons.OKCancel, System.Windows.Forms.MessageBoxIcon.None); } |
◆実行結果

▼ページトップへ
◆8.再試行とキャンセルボタンにする
private void button1_Click(object sender, EventArgs e) { MessageBox.Show(this, "メッセージの内容", "Title", System.Windows.Forms.MessageBoxButtons.RetryCancel, System.Windows.Forms.MessageBoxIcon.None); } |
◆実行結果

▼ページトップへ
◆9.「はい」ボタンと「いいえ」ボタンにする
private void button1_Click(object sender, EventArgs e) { MessageBox.Show(this, "メッセージの内容", "Title", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.None); } |
◆実行結果

▼ページトップへ
◆10.「はい」ボタンと「いいえ」ボタン、「キャンセル」ボタンにする
private void button1_Click(object sender, EventArgs e) { MessageBox.Show(this, "メッセージの内容", "Title", System.Windows.Forms.MessageBoxButtons.YesNoCancel, System.Windows.Forms.MessageBoxIcon.None); } |
◆実行結果

▼ページトップへ
◆11.「中止」ボタンと「再試行」ボタン、「無視」ボタンにする
private void button1_Click(object sender, EventArgs e) { MessageBox.Show(this, "メッセージの内容", "Title", System.Windows.Forms.MessageBoxButtons.AbortRetryIgnore, System.Windows.Forms.MessageBoxIcon.None); } |
◆実行結果

▼ページトップへ