LoginSignup
1
1

More than 5 years have passed since last update.

(C#)Formが持っている子画面の数を取得する

Posted at

共通フォームを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
            }
        }
    }
}
1
1
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
1