LoginSignup
1
3

More than 3 years have passed since last update.

C# - モーダルなサブウィンドウを開くサンプルコードべた書き(Visual Studio不使用)

Last updated at Posted at 2020-10-29

Formの画面レイアウトが一杯になってきて設定画面を切り離したいときとかに、子ウィンドウを出したくなると思います。
今回はモーダルウィンドウ(閉じるまで親のFormを操作できない)を表示させます。

モーダルウィンドウ モードレスウィンドウ - Google検索

画面キャプチャ

image.png

サンプルソースコード


using System;
using System.Drawing;
using System.Windows.Forms;

class MainForm : Form
{
    MainForm()
    {
        Text = "Main window";
        ClientSize = new Size(400,300);

        var btn = new Button(){
            Size = new Size(200,50),
            Text = "Show sub window.",
        };
        Controls.Add(btn);
        btn.Click += (s,e)=>{ ShowSubFormWithText("this is a sample."); };
    }

    static void ShowSubFormWithText(string text)
    {
        SubForm f = new SubForm(text);
        f.ShowDialog();
    }

    [STAThread]
    static void Main(string[] args)
    {
        Application.Run(new MainForm());
    }
}


class SubForm : Form
{
    public SubForm(string text)
    {
        Text = "Sub window";
        ClientSize = new Size(300,150);
        StartPosition = FormStartPosition.CenterParent;
        // StartPosition = FormStartPosition.CenterScreen;

        var txt = new TextBox(){
            Text = text,
            Multiline = true,
            ScrollBars = ScrollBars.Both,
            Dock = DockStyle.Fill
        };

        txt.KeyDown += (sender,e)=>{
            if (e.Control && e.KeyCode == Keys.A) { txt.SelectAll(); }
        };

        Controls.Add(txt);
    }
}

サブウィンドウから結果データを受け取りたい場合

参考サイト2を参考に作成しました。


using System;
using System.Drawing;
using System.Windows.Forms;

class MainForm : Form
{
    MainForm()
    {
        Text = "Main window";
        ClientSize = new Size(400,300);

        var btn = new Button(){
            Size = new Size(200,50),
            Text = "Show sub window.",
        };
        Controls.Add(btn);
        btn.Click += (s,e)=>{ ShowSubFormWithText("this is a sample."); };
    }

    static void ShowSubFormWithText(string text)
    {
        SubForm f = new SubForm(text);
        DialogResult res = f.ShowDialog();
        Console.WriteLine(res); // DialogResultの確認用
        Console.WriteLine(f.Value); // 結果データの確認用

        if ( res == DialogResult.OK ) {
            // ... OKが押されたときの処理
        }
    }

    [STAThread]
    static void Main(string[] args)
    {
        Application.Run(new MainForm());
    }
}


class SubForm : Form
{
    public string Value;
    TextBox txt;

    public SubForm(string text)
    {
        Value = "";

        Text = "Sub window";
        ClientSize = new Size(300,200);
        StartPosition = FormStartPosition.CenterParent;
        // FormBorderStyle = FormBorderStyle.FixedSingle; // サイズ変更不可にする場合

        txt = new TextBox(){
            Location =new Point(0, 0),
            Size = new Size(300, 160),
            Text = text,
            Multiline = true,
            ScrollBars = ScrollBars.Both,
        };
        txt.KeyDown += (s,e)=>{
            if (e.Control && e.KeyCode == Keys.A) { txt.SelectAll(); }
        };

        Controls.Add(txt);


        var btn1 = new Button(){
            Location =new Point(0, 160),
            Size = new Size(150, 40),
            Text = "OK",
        };
        var btn2 = new Button(){
            Location =new Point(150, 160),
            Size = new Size(150, 40),
            Text = "Cancel",
        };
        btn1.Click += (s,e)=>{
            DialogResult = DialogResult.OK;
            Close();
        };
        btn2.Click += (s,e)=>{
            DialogResult = DialogResult.Cancel;
            Close();
        };
        Controls.Add(btn1);
        Controls.Add(btn2);


        FormClosed += (s,e)=>{Value = txt.Text;}; // SubFormが閉じられたときにデータをセットしておく
    }
}

サブウィンドウfが閉じて(FormClosed)からも、fを参照している限りはガベージコレクションの対象にはならないので、fのフィールドValueを取得することができる1
という理屈のはず。

参考サイト

  1. フォームを画面の真ん中(あるいは、任意の位置)に表示する - .NET Tips (VB.NET,C#...)
  2. モーダルフォームとの間でデータや値の受け渡しをする - C#プログラミング

  1. 直接フィールドを参照するのはキモチワルイ気がするが、できるだけシンプルなサンプルになるようにフィールドを使用した。 

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3