C# のメッセージボックス

Muhammad Maisam Abbas 2021年5月23日 2021年5月9日
C# のメッセージボックス

このチュートリアルでは、C# ではいまたはいいえオプションを使用してメッセージボックスを作成する方法について説明します。

C# で MessageBox クラスを使用してメッセージボックスを作成する

MessageBox クラスは、C# でメッセージウィンドウを表示します。yes および no オプションを使用してメッセージボックスを作成する場合は、MessageBox クラスコンストラクターのパラメーターに MessageBoxButton.YesNo 列挙型を渡すことができます。次のコード例は、C# の MessageBox クラスで yes および no オプションを使用してメッセージボックスを作成する方法を示しています。

using System.Windows.Forms;

namespace messagbox
{
    static class Program
    {
        static void Main()
        {

            Application.EnableVisualStyles();
            DialogResult dr = MessageBox.Show("Are you happy now?",
                      "Mood Test", MessageBoxButtons.YesNo);
            switch (dr)
            {
                case DialogResult.Yes:
                    MessageBox.Show("That is Fantastic");
                    break;
                case DialogResult.No:
                    MessageBox.Show("Why Not?");
                    break;
            }
        }
    }
}

出力:

C# メッセージボックス 1

C# メッセージボックス 2

上記のコードでは、MessageBox.Show() 関数の結果を DialogResult 列挙型の dr インスタンス内に格納しました。DialogResult 列挙型には、ダイアログボックスによって返される値を表す識別子が含まれています。switch ステートメントを使用して、MessageBox.Show() 関数によって返される値を確認し、選択したオプションごとに別のメッセージボックスを表示しました。

Muhammad Maisam Abbas avatar Muhammad Maisam Abbas avatar

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.

LinkedIn

関連記事 - Csharp GUI