Windowのオーナー設定を行った場合、子ウィンドウは必ず親ウィンドウより前に表示されます。
また、どちらかのWindowを最小化すると、他のWindowも必ず最小化されてしまうなど、連動してしまう部分があります。
連動しない2つのWindow間でデータをやりとりする方法としては、例えば以下のように子ウィンドウ側に親ウィンドウを保持するためのフィールドを持つ方法が考えられます。
この場合親ウィンドウ側では、子ウィンドウを生成したときに子ウィンドウに対して自分自身を設定する必要があります。
-------------------------------------------------------
using System;
using System.Windows.Forms;
class WinSample : Form
{
private Button mybutton;
private TextBox myTextBox;
public string DispText
{
get { return this.myTextBox.Text; }
set { this.myTextBox.Text = value; }
}
public static void Main()
{
Application.Run(new WinSample());
}
public WinSample()
{
this.Text = "親ウィンドウ";
this.myTextBox = new TextBox();
this.myTextBox.Location = new System.Drawing.Point(100, 100);
this.Controls.Add(myTextBox);
this.mybutton = new Button();
this.mybutton.Location = new System.Drawing.Point(200, 200);
this.mybutton.Text = "子ウィンドウ";
this.mybutton.Click += new EventHandler(this.button_Click);
this.Controls.Add(mybutton);
}
private void button_Click(object sender, EventArgs e)
{
WinSample2 ws2 = new WinSample2();
// 子ウィンドウのプロパティに自分を登録
ws2.Ws = this;
ws2.DispText2 = myTextBox.Text;
ws2.Show();
}
}
class WinSample2 : Form
{
private Button mybutton2;
private Button mybutton3;
private TextBox myTextBox2;
// 親ウィンドウを保持するフィールド
private WinSample ws;
// 親ウィンドウを設定するためのプロパティ
public WinSample Ws
{
set { this.ws = value; }
}
public string DispText2
{
set { this.myTextBox2.Text = value; }
}
public WinSample2()
{
this.Text = "子ウィンドウ";
this.myTextBox2 = new TextBox();
this.myTextBox2.Location = new System.Drawing.Point(100, 100);
this.Controls.Add(myTextBox2);
this.mybutton2 = new Button();
this.mybutton2.Location = new System.Drawing.Point(200, 180);
this.mybutton2.Text = "親から取得";
this.mybutton2.Click += new EventHandler(this.button2_Click);
this.Controls.Add(mybutton2);
this.mybutton3 = new Button();
this.mybutton3.Location = new System.Drawing.Point(200, 230);
this.mybutton3.Text = "親へ設定";
this.mybutton3.Click += new EventHandler(this.button3_Click);
this.Controls.Add(mybutton3);
}
private void button2_Click(object sender, EventArgs e)
{
// 親ウィンドウのプロパティから値を取り出す
this.myTextBox2.Text = this.ws.DispText;
}
private void button3_Click(object sender, EventArgs e)
{
// 親ウィンドウのプロパティに値を設定する
this.ws.DispText = this.myTextBox2.Text;
}
}
-------------------------------------------------------
あおい情報システム株式会社 小野修司(どっとねっとふぁん)