12
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Form上の全てのコントロールを取得

Last updated at Posted at 2014-01-13

概要

WindowsForm上の全てのコントロールを取得します。

定義メソッド

/// /// 指定のコントロール上の全てのジェネリック型 Tコントロールを取得する。 /// /// 対象となる型 /// 指定のコントロール /// 指定のコントロール上の全てのジェネリック型 Tコントロールのインスタンス public static List GetAllControls(Control top) where T : Control { List buf = new List(); foreach (Control ctrl in top.Controls) { if(ctrl is T) buf.Add((T)ctrl); buf.AddRange(GetAllControls(ctrl)); } return buf; }

使用コード

// コントロール全てを列挙 List controls = GetAllControls(this); System.Console.WriteLine("controls : " + controls.Count().ToString()); foreach (var ctl in controls) { System.Console.WriteLine("control : " + ctl.Name); }
        // ボタン、またはボタンを継承したコントロールを列挙
        List<Button> buttons = GetAllControls<Button>(this);
        System.Console.WriteLine("buttons : " + buttons.Count().ToString());
        foreach (var ctl in buttons)
        {
            System.Console.WriteLine("button : " + ctl.Name);
        }

参考

http://www.atmarkit.co.jp/fdotnet/dotnettips/224controls/controls.html
12
17
4

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
12
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?