宇宙仮面の
C# Programming

 
Image

MessageBox


開発環境: Visual Studio 2003
MessageBox 表示例
Image
これを表示するのに必要なものは、次の1行だけ。
DialogResult res = MessageBox.Show(
"Enter Text here",  // テキスト
this.Text, // キャプション
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Exclamation);
MessageBoxButtons説明
AbortRetryIgnore[中止] ボタン、[再試行] ボタン、および [無視] ボタンを表示する。
OK[OK] ボタンを表示する。
OKCancel[OK] ボタンと [キャンセル] ボタンを表示する。
RetryCancel[再試行] ボタンと [キャンセル] ボタンを表示する。
YesNo[はい] ボタンと [いいえ] ボタンを表示する。
YesNoCancel[はい] ボタン、[いいえ] ボタン、および [キャンセル] ボタンを表示する。
MessageBoxIcon説明
Asterisk または Information円で囲まれた小文字の i から成る記号を表示する。
Error または Hand または Stop背景が赤の円で囲まれた白い X から成る記号を表示表示する。
Exclamation または Warning背景が黄色の三角形で囲まれた感嘆符から成る記号を表示表示する。
None記号を表示しない。
Question円で囲まれた疑問符から成る記号を表示します。
MessageBox Test Dialog
次は、メッセージボックスのテスト用ダイアログです。
これで一応すべての組み合わせを確認できます。
Image

以下、MessageBox Test Dialog のソースコード。


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace TestMessageBox
{
        /// <summary>
        /// Form1 の概要の説明です。
        /// </summary>
        public class TestMessageBox : System.Windows.Forms.Form
        {
                private System.Windows.Forms.Button button1;
                private System.Windows.Forms.RadioButton radioButtonOK;
                private System.Windows.Forms.RadioButton radioButtonYesNoCancel;
                private System.Windows.Forms.RadioButton radioButtonOKCancel;
                private System.Windows.Forms.RadioButton radioButtonYesNo;
                private System.Windows.Forms.RadioButton radioButtonExclamation;
                private System.Windows.Forms.RadioButton radioButtonError;
                private System.Windows.Forms.RadioButton radioButtonInformation;
                private System.Windows.Forms.RadioButton radioButtonNoIcon;
                private System.Windows.Forms.RadioButton radioButtonQuestion;
                private System.Windows.Forms.Label label1;
                private System.Windows.Forms.Label label2;
                private System.Windows.Forms.Label label3;
                private System.Windows.Forms.Label label4;
                private System.Windows.Forms.TextBox text;
                private System.Windows.Forms.TextBox caption;
                private System.Windows.Forms.TextBox dialogResult;
                private System.Windows.Forms.Label label5;
                private System.Windows.Forms.GroupBox groupBox_MessegeBoxButtons;
                private System.Windows.Forms.GroupBox groupBox_MessageBoxIcon;
                /// <summary>
                /// 必要なデザイナ変数です。
                /// </summary>
                private System.ComponentModel.Container components = null;

                public TestMessageBox()
                {
                        //
                        // Windows フォーム デザイナ サポートに必要です。
                        //
                        InitializeComponent();

                        //
                        // TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
                        //
                }

                /// <summary>
                /// 使用されているリソースに後処理を実行します。
                /// </summary>
                protected override void Dispose( bool disposing )
                {
                        if( disposing )
                        {
                                if (components != null) 
                                {
                                        components.Dispose();
                                }
                        }
                        base.Dispose( disposing );
                }

                #region Windows Form Designer generated code
                /// <summary>
                /// デザイナ サポートに必要です。このメソッドの内容を
                /// コード エディタで変更しないでください。
                /// </summary>
                private void InitializeComponent()
                {
                        System.Collections.Specialized.NameValueCollection configurationAppSettings = System.Configuration.ConfigurationSettings.AppSettings;
                        this.button1 = new System.Windows.Forms.Button();
                        this.text = new System.Windows.Forms.TextBox();
                        this.groupBox_MessegeBoxButtons = new System.Windows.Forms.GroupBox();
                        this.radioButtonOK = new System.Windows.Forms.RadioButton();
                        this.radioButtonYesNoCancel = new System.Windows.Forms.RadioButton();
                        this.radioButtonOKCancel = new System.Windows.Forms.RadioButton();
                        this.radioButtonYesNo = new System.Windows.Forms.RadioButton();
                        this.groupBox_MessageBoxIcon = new System.Windows.Forms.GroupBox();
                        this.radioButtonError = new System.Windows.Forms.RadioButton();
                        this.radioButtonExclamation = new System.Windows.Forms.RadioButton();
                        this.radioButtonInformation = new System.Windows.Forms.RadioButton();
                        this.radioButtonNoIcon = new System.Windows.Forms.RadioButton();
                        this.radioButtonQuestion = new System.Windows.Forms.RadioButton();
                        this.caption = new System.Windows.Forms.TextBox();
                        this.label1 = new System.Windows.Forms.Label();
                        this.label2 = new System.Windows.Forms.Label();
                        this.label3 = new System.Windows.Forms.Label();
                        this.label4 = new System.Windows.Forms.Label();
                        this.dialogResult = new System.Windows.Forms.TextBox();
                        this.label5 = new System.Windows.Forms.Label();
                        this.groupBox_MessegeBoxButtons.SuspendLayout();
                        this.groupBox_MessageBoxIcon.SuspendLayout();
                        this.SuspendLayout();
                        // 
                        // button1
                        // 
                        this.button1.Font = new System.Drawing.Font("MS UI Gothic", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
                        this.button1.Location = new System.Drawing.Point(568, 32);
                        this.button1.Name = "button1";
                        this.button1.Size = new System.Drawing.Size(104, 32);
                        this.button1.TabIndex = 1;
                        this.button1.Text = "Show";
                        this.button1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.button1MousePressed);
                        // 
                        // text
                        // 
                        this.text.Font = new System.Drawing.Font("MS UI Gothic", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
                        this.text.Location = new System.Drawing.Point(96, 80);
                        this.text.Name = "text";
                        this.text.ReadOnly = bool.Parse(configurationAppSettings.Get("textBox.ReadOnly"));
                        this.text.Size = new System.Drawing.Size(152, 23);
                        this.text.TabIndex = 0;
                        this.text.Text = configurationAppSettings.Get("textBox.Text");
                        // 
                        // groupBox_MessegeBoxButtons
                        // 
                        this.groupBox_MessegeBoxButtons.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                                                                                                                                         this.radioButtonOK,
                                                                                                                                                                                                         this.radioButtonYesNoCancel,
                                                                                                                                                                                                         this.radioButtonOKCancel,
                                                                                                                                                                                                         this.radioButtonYesNo});
                        this.groupBox_MessegeBoxButtons.Location = new System.Drawing.Point(512, 72);
                        this.groupBox_MessegeBoxButtons.Name = "groupBox_MessegeBoxButtons";
                        this.groupBox_MessegeBoxButtons.Size = new System.Drawing.Size(168, 160);
                        this.groupBox_MessegeBoxButtons.TabIndex = 2;
                        this.groupBox_MessegeBoxButtons.TabStop = false;
                        this.groupBox_MessegeBoxButtons.Text = "MessageBoxButtons";
                        // 
                        // radioButtonOK
                        // 
                        this.radioButtonOK.Location = new System.Drawing.Point(32, 24);
                        this.radioButtonOK.Name = "radioButtonOK";
                        this.radioButtonOK.Size = new System.Drawing.Size(48, 24);
                        this.radioButtonOK.TabIndex = 0;
                        this.radioButtonOK.Text = "OK";
                        // 
                        // radioButtonYesNoCancel
                        // 
                        this.radioButtonYesNoCancel.Location = new System.Drawing.Point(32, 120);
                        this.radioButtonYesNoCancel.Name = "radioButtonYesNoCancel";
                        this.radioButtonYesNoCancel.Size = new System.Drawing.Size(128, 16);
                        this.radioButtonYesNoCancel.TabIndex = 3;
                        this.radioButtonYesNoCancel.Text = "YesNoCancel";
                        // 
                        // radioButtonOKCancel
                        // 
                        this.radioButtonOKCancel.Location = new System.Drawing.Point(32, 56);
                        this.radioButtonOKCancel.Name = "radioButtonOKCancel";
                        this.radioButtonOKCancel.Size = new System.Drawing.Size(96, 24);
                        this.radioButtonOKCancel.TabIndex = 1;
                        this.radioButtonOKCancel.Text = "OKCancel";
                        // 
                        // radioButtonYesNo
                        // 
                        this.radioButtonYesNo.Location = new System.Drawing.Point(32, 88);
                        this.radioButtonYesNo.Name = "radioButtonYesNo";
                        this.radioButtonYesNo.Size = new System.Drawing.Size(80, 24);
                        this.radioButtonYesNo.TabIndex = 2;
                        this.radioButtonYesNo.Text = "YesNo";
                        // 
                        // groupBox_MessageBoxIcon
                        // 
                        this.groupBox_MessageBoxIcon.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                                                                                                                                  this.radioButtonQuestion,
                                                                                                                                                                                                  this.radioButtonNoIcon,
                                                                                                                                                                                                  this.radioButtonInformation,
                                                                                                                                                                                                  this.radioButtonExclamation,
                                                                                                                                                                                                  this.radioButtonError});
                        this.groupBox_MessageBoxIcon.Location = new System.Drawing.Point(312, 72);
                        this.groupBox_MessageBoxIcon.Name = "groupBox_MessageBoxIcon";
                        this.groupBox_MessageBoxIcon.Size = new System.Drawing.Size(184, 184);
                        this.groupBox_MessageBoxIcon.TabIndex = 3;
                        this.groupBox_MessageBoxIcon.TabStop = false;
                        this.groupBox_MessageBoxIcon.Text = "MessageBoxIcon";
                        // 
                        // radioButtonError
                        // 
                        this.radioButtonError.Location = new System.Drawing.Point(24, 56);
                        this.radioButtonError.Name = "radioButtonError";
                        this.radioButtonError.Size = new System.Drawing.Size(136, 24);
                        this.radioButtonError.TabIndex = 0;
                        this.radioButtonError.Text = "Error or Hand or Stop";
                        // 
                        // radioButtonExclamation
                        // 
                        this.radioButtonExclamation.Location = new System.Drawing.Point(24, 88);
                        this.radioButtonExclamation.Name = "radioButtonExclamation";
                        this.radioButtonExclamation.Size = new System.Drawing.Size(152, 24);
                        this.radioButtonExclamation.TabIndex = 0;
                        this.radioButtonExclamation.Text = "Exclamation or Warning";
                        // 
                        // radioButtonInformation
                        // 
                        this.radioButtonInformation.Location = new System.Drawing.Point(24, 120);
                        this.radioButtonInformation.Name = "radioButtonInformation";
                        this.radioButtonInformation.Size = new System.Drawing.Size(144, 24);
                        this.radioButtonInformation.TabIndex = 0;
                        this.radioButtonInformation.Text = "Information or Asterisk";
                        // 
                        // radioButtonNoIcon
                        // 
                        this.radioButtonNoIcon.Location = new System.Drawing.Point(24, 32);
                        this.radioButtonNoIcon.Name = "radioButtonNoIcon";
                        this.radioButtonNoIcon.Size = new System.Drawing.Size(104, 16);
                        this.radioButtonNoIcon.TabIndex = 0;
                        this.radioButtonNoIcon.Text = "None";
                        // 

                        // radioButtonQuestion
                        // 
                        this.radioButtonQuestion.Location = new System.Drawing.Point(24, 144);
                        this.radioButtonQuestion.Name = "radioButtonQuestion";
                        this.radioButtonQuestion.Size = new System.Drawing.Size(144, 24);
                        this.radioButtonQuestion.TabIndex = 0;
                        this.radioButtonQuestion.Text = "Question";
                        // 
                        // caption
                        // 
                        this.caption.Font = new System.Drawing.Font("MS UI Gothic", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
                        this.caption.Location = new System.Drawing.Point(96, 128);
                        this.caption.Name = "caption";
                        this.caption.Size = new System.Drawing.Size(152, 23);
                        this.caption.TabIndex = 0;
                        this.caption.Text = "Enter Caption here";
                        // 
                        // label1
                        // 
                        this.label1.Location = new System.Drawing.Point(16, 80);
                        this.label1.Name = "label1";
                        this.label1.Size = new System.Drawing.Size(40, 24);
                        this.label1.TabIndex = 4;
                        this.label1.Text = "Text";
                        // 
                        // label2
                        // 
                        this.label2.Location = new System.Drawing.Point(16, 128);
                        this.label2.Name = "label2";
                        this.label2.Size = new System.Drawing.Size(48, 24);
                        this.label2.TabIndex = 4;
                        this.label2.Text = "Caption";
                        // 
                        // label3
                        // 
                        this.label3.Location = new System.Drawing.Point(24, 16);
                        this.label3.Name = "label3";
                        this.label3.Size = new System.Drawing.Size(208, 24);
                        this.label3.TabIndex = 4;
                        this.label3.Text = "Namespace: System.Windows.Forms";
                        // 
                        // label4
                        // 
                        this.label4.Font = new System.Drawing.Font("MS UI Gothic", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
                        this.label4.Location = new System.Drawing.Point(24, 40);
                        this.label4.Name = "label4";
                        this.label4.Size = new System.Drawing.Size(504, 32);
                        this.label4.TabIndex = 4;
                        this.label4.Text = "MessageBox.Show( string Text, string Caption, MessageBoxIcon, MessageBoxButtons);" +
                                "";
                        // 
                        // dialogResult
                        // 
                        this.dialogResult.Font = new System.Drawing.Font("MS UI Gothic", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
                        this.dialogResult.Location = new System.Drawing.Point(96, 208);
                        this.dialogResult.Name = "dialogResult";
                        this.dialogResult.Size = new System.Drawing.Size(152, 23);
                        this.dialogResult.TabIndex = 0;
                        this.dialogResult.Text = "";
                        // 
                        // label5
                        // 
                        this.label5.Location = new System.Drawing.Point(16, 216);
                        this.label5.Name = "label5";
                        this.label5.Size = new System.Drawing.Size(72, 24);
                        this.label5.TabIndex = 4;
                        this.label5.Text = "DialogResult";
                        // 
                        // TestMessageBox
                        // 
                        this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
                        this.ClientSize = new System.Drawing.Size(712, 270);
                        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                                                                                  this.label5,
                                                                                                                                                  this.groupBox_MessegeBoxButtons});
                        this.Name = "TestMessageBox";
                        this.Text = ".Net FrameWork MessageBox";
                        this.groupBox_MessegeBoxButtons.ResumeLayout(false);
                        this.groupBox_MessageBoxIcon.ResumeLayout(false);
                        this.ResumeLayout(false);

                }
                #endregion

                /// <summary>
                /// アプリケーションのメイン エントリ ポイントです。
                /// </summary>
                [STAThread]
                static void Main() 
                {
                        Application.Run(new TestMessageBox());
                }

                private void button1MousePressed(object sender, System.Windows.Forms.MouseEventArgs e)
                {
                        DialogResult res = MessageBox.Show(text.Text, caption.Text, GetButonType(), GetButonIcon());
                        dialogResult.Text = res.ToString();
                }

                private MessageBoxButtons GetButonType()
                {
                        if ( radioButtonOK.Checked ) return MessageBoxButtons.OK;
                        if ( radioButtonYesNo.Checked ) return MessageBoxButtons.YesNo;
                        if ( radioButtonYesNoCancel.Checked ) return MessageBoxButtons.YesNoCancel;
                        if ( radioButtonOKCancel.Checked ) return MessageBoxButtons.OKCancel;
                        return MessageBoxButtons.OK;
                }

                private MessageBoxIcon GetButonIcon()
                {
                        if ( radioButtonExclamation.Checked ) return MessageBoxIcon.Exclamation;
                        if ( radioButtonError.Checked ) return MessageBoxIcon.Error;
                        if ( radioButtonInformation.Checked ) return MessageBoxIcon.Information;
                        if ( radioButtonQuestion.Checked ) return MessageBoxIcon.Question;
                        return MessageBoxIcon.Error;
                }

        }
}