中堅プログラマーの備忘録

忘れっぽくなってきたので備忘録として・・・

【C#.net】フォーム間でのデータの受け渡し方法のサンプル

1.概要

Formアプリケーションを作成する際に
複数のFormを扱う場面があるかと思います。
そんな時に使えるForm間のデータやり取りの方法についてまとめてみます。
方法はたくさんあるかと思いますが
今回は3パターンについて記載していこうと思います。

事前準備としては任意のプロジェクトを作成し【Form1】と【Form2】を用意します。
【Form2】は【Form1】からモーダルとして呼ばれることが前提のサンプルになっています。

2.親Formから子Formにデータを渡す

単純に子Formを起動した時にデータを渡す方法です。
正確な表現をするとpublicコントロールに値を設定するといった感じでしょうか?

【Form1】には【TextBox1】と【Button1】

【Form2】には【TextBox1】

を貼り付けます。

この時【Form2】の【TextBox1】は【public】としておきます。
f:id:tsu--kun:20191126161545p:plain


下記はForm1のスクリプトです。
※Form2のスクリプトはありません。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FormToForm
{
    public partial class Form1 : Form
    {
        Form2 f2;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            f2 = new Form2();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            f2.textBox1.Text = textBox1.Text;
            f2.ShowDialog();
        }
    }
}

実行結果

f:id:tsu--kun:20191126161600g:plain

3.子フォームの操作結果を取得する

子FormにButtonを2つ貼り付けて【DialogResult】を設定し
操作結果を取得する方法になります。

【Form1】には【TextBox1】と【Button1】

【Form2】には【Button1】と【Button2】

を貼り付けます。

以下スクリプト。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FormToForm
{
    public partial class Form1 : Form
    {
        Form2 f2;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            f2 = new Form2();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            f2.ShowDialog();
            if (f2.DialogResult == DialogResult.OK)
            {
                textBox1.Text = "OK";
            }
            else if (f2.DialogResult == DialogResult.Cancel)
            {
                textBox1.Text = "Cancel";
            }
        }
    }
}


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FormToForm
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

    }
}

実行結果

f:id:tsu--kun:20191126161620g:plain

4.親子間でデータをやり取りする

子フォームにpublic変数を用意し、これを使ってデータのやり取りを行います。

【【Form1】と【Form2】には【TextBox1】と【Button1】

を貼り付けます。

以下スクリプトです。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FormToForm
{
    public partial class Form1 : Form
    {
        Form2 f2;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            f2 = new Form2();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            f2.testData = "おはよう";
            f2.ShowDialog();
            textBox1.Text = f2.testData;
        }
    }
}


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FormToForm
{
    public partial class Form2 : Form
    {
        private string _testData;
        public string testData
        {
            get
            {
                return _testData;
            }
            set
            {
                _testData = value;
            }
        }

        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            textBox1.Text = testData;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            testData = "おやすみ";
            this.Close();
        }
    }
}

実行結果

f:id:tsu--kun:20191126165004g:plain