PR

【C#】でダイアログ表示(ShowDialog)

【C#】
広告

はじめに

C#におけるダイアログ表示は、ユーザーインターフェースを構築する上で重要な役割を果たします。特に、ユーザーの入力を求める場面や、確認・警告メッセージを表示する場合に頻繁に使用されます。本記事では、C#のShowDialogメソッドを使用してダイアログを表示する方法について、基本から応用までを具体的なコード例を交えながら解説します。初心者の方にも分かりやすいように、ステップバイステップで説明していきます。

ダイアログの基本

ダイアログの種類

C#では、標準のダイアログとして以下の種類があります。

  • メッセージボックス
  • フォーム
  • ファイルダイアログ(OpenFileDialog、SaveFileDialog)
  • カラーダイアログ(ColorDialog)
  • フォントダイアログ(FontDialog)

ShowDialogメソッドの基本

ShowDialogメソッドは、モーダルダイアログを表示するために使用されます。モーダルダイアログは、ユーザーがそのダイアログを閉じるまで他のウィンドウと対話できなくなるダイアログです。以下は、基本的なShowDialogメソッドの使用例です。

using System;
using System.Windows.Forms;

public class MyForm : Form
{
    public MyForm()
    {
        Button button = new Button();
        button.Text = "Show Dialog";
        button.Click += (sender, e) => {
            Form dialog = new Form();
            dialog.Text = "My Dialog";
            dialog.ShowDialog();
        };
        Controls.Add(button);
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MyForm());
    }
}

この例では、ボタンをクリックすると新しいフォームがダイアログとして表示されます。

メッセージボックスの表示

メッセージボックスの基本的な使い方

メッセージボックスは、簡単なメッセージをユーザーに伝えるために使用されます。以下は、メッセージボックスを表示する例です。

using System;
using System.Windows.Forms;

public class MyForm : Form
{
    public MyForm()
    {
        Button button = new Button();
        button.Text = "Show MessageBox";
        button.Click += (sender, e) => {
            MessageBox.Show("Hello, this is a message box!");
        };
        Controls.Add(button);
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MyForm());
    }
}

メッセージボックスのオプション

メッセージボックスには、ボタンやアイコンを追加することができます。

using System;
using System.Windows.Forms;

public class MyForm : Form
{
    public MyForm()
    {
        Button button = new Button();
        button.Text = "Show MessageBox";
        button.Click += (sender, e) => {
            MessageBox.Show("Do you want to continue?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        };
        Controls.Add(button);
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MyForm());
    }
}

この例では、YesボタンとNoボタン、質問アイコンを持つメッセージボックスを表示します。

フォームのダイアログ表示

カスタムダイアログの作成

カスタムダイアログを作成するには、新しいフォームを作成し、それをダイアログとして表示します。

using System;
using System.Windows.Forms;

public class MyForm : Form
{
    public MyForm()
    {
        Button button = new Button();
        button.Text = "Show Custom Dialog";
        button.Click += (sender, e) => {
            CustomDialog dialog = new CustomDialog();
            dialog.ShowDialog();
        };
        Controls.Add(button);
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MyForm());
    }
}

public class CustomDialog : Form
{
    public CustomDialog()
    {
        Text = "Custom Dialog";
        Button okButton = new Button();
        okButton.Text = "OK";
        okButton.DialogResult = DialogResult.OK;
        Controls.Add(okButton);
    }
}

ダイアログ結果の取得

ダイアログが閉じられたときに、どのボタンが押されたかを取得することができます。

using System;
using System.Windows.Forms;

public class MyForm : Form
{
    public MyForm()
    {
        Button button = new Button();
        button.Text = "Show Custom Dialog";
        button.Click += (sender, e) => {
            CustomDialog dialog = new CustomDialog();
            DialogResult result = dialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                MessageBox.Show("OK was clicked.");
            }
            else
            {
                MessageBox.Show("Dialog was closed.");
            }
        };
        Controls.Add(button);
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MyForm());
    }
}

public class CustomDialog : Form
{
    public CustomDialog()
    {
        Text = "Custom Dialog";
        Button okButton = new Button();
        okButton.Text = "OK";
        okButton.DialogResult = DialogResult.OK;
        Controls.Add(okButton);
    }
}

ファイルダイアログの使用

OpenFileDialogの使用

OpenFileDialogは、ファイルを開くためのダイアログです。

using System;
using System.Windows.Forms;

public class MyForm : Form
{
    public MyForm()
    {
        Button button = new Button();
        button.Text = "Open File";
        button.Click += (sender, e) => {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                MessageBox.Show($"File selected: {openFileDialog.FileName}");
            }
        };
        Controls.Add(button);
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MyForm());
    }
}

SaveFileDialogの使用

SaveFileDialogは、ファイルを保存するためのダイアログです。

using System;
using System.Windows.Forms;

public class MyForm : Form
{
    public MyForm()
    {
        Button button = new Button();
        button.Text = "Save File";
        button.Click += (sender, e) => {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                MessageBox.Show($"File saved: {saveFileDialog.FileName}");
            }
        };
        Controls.Add(button);
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MyForm());
    }
}

その他のダイアログ

カラーダイアログの使用

ColorDialogは、色を選択するためのダイアログです。

using System;
using System.Windows.Forms;

public class MyForm : Form
{
    public MyForm()
    {
        Button button = new Button();
        button.Text = "Choose Color";
        button.Click += (sender, e) => {
            ColorDialog colorDialog = new ColorDialog();
            if (colorDialog.ShowDialog() == DialogResult.OK)
            {
                this.BackColor = colorDialog.Color;
            }
        };
        Controls.Add(button);
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MyForm());
    }
}

フォントダイアログの使用

FontDialogは、フォントを選択するためのダイアログです。

using System;
using System.Windows.Forms;

public class MyForm : Form
{
    public MyForm()
    {
        Button button = new Button();
        button.Text = "Choose Font";
        button.Click += (sender, e) => {
            FontDialog fontDialog = new FontDialog();
            if (fontDialog.ShowDialog() == DialogResult.OK)
            {
                this.Font = fontDialog.Font;
            }
        };
        Controls.Add(button);
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MyForm());
    }
}

まとめ

本記事では、C#におけるダイアログ表示について、ShowDialogメソッドを中心に解説しました。メッセージボックス、カスタムダイアログ、ファイルダイアログ、カラーダイアログ、フォントダイアログなど、様々なダイアログの使用方法を具体的なコード例とともに紹介しました。適切にダイアログを活用することで、ユーザーインターフェースをより使いやすくすることができます。ダイアログの使用方法をマスターし、効果的なアプリケーション開発に役立ててください。

広告
【C#】
広告
タイトルとURLをコピーしました