共通フォームをOwnerとして、複数のフォームを作成。
Ownerとして設定されている画面がどのフォームのOwnerとして設定されているかが知りたかった。
取得方法
this.OwnedForms
を用いて取得できました。テストコード
using System.Windows.Forms;
namespace testFormApp
{
public partial class TestForm : Form
{
public TestForm()
{
InitializeComponent();
CreateChildren();
OutputChidrenInfo();
}
private void CreateChildren()
{
Form c1 = new Form {Text = "this is c1"};
Form c2 = new Form {Text = "this is c2"};
Form c3 = new Form {Text = "this is c3"};
c1.Owner = this;
c2.Owner = this;
c3.Owner = this;
}
private void OutputChidrenInfo()
{
// どれだけの画面からOwnerに指定されているか
int form_num = this.OwnedForms.Length;
MessageBox.Show("children form : " + form_num.ToString());
// メッセージボックスに表示される結果
// children form : 3
Form[] form_info = this.OwnedForms;
for (int i = 0; i < form_info.Length; i++)
{
MessageBox.Show(form_info[i].Text);
// メッセージボックスに表示される結果
// this is c1
// this is c2
// this is c3
}
}
}
}
- 参考サイト