Created
March 15, 2018 04:59
-
-
Save juner/508fc79a8df4d680572f1f0489a0ae2e to your computer and use it in GitHub Desktop.
WinForms ControlExtensions Parents() AndParents()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Windows.Forms; | |
using System.Collections.Generic; | |
namespace System.Windows.Forms.Extensions | |
{ | |
/// <summary> | |
/// コントロールの拡張関数クラス | |
/// </summary> | |
public static class ControlExtension | |
{ | |
/// <summary> | |
/// 自分の親を返す | |
/// </summary> | |
/// <param name="self"></param> | |
/// <returns></returns> | |
public static IEnumerable<Control> Parents(this Control self) | |
{ | |
if (self == null) | |
yield break; | |
var c = self; | |
while((c = c.Parent) != null) | |
{ | |
yield return c; | |
} | |
} | |
/// <summary> | |
/// 自分を含めた自分の親を返す | |
/// </summary> | |
/// <param name="self"></param> | |
/// <returns></returns> | |
public static IEnumerable<Control> AndParents(this Control self) | |
{ | |
if (self == null) | |
yield break; | |
var c = self; | |
do | |
{ | |
yield return c; | |
} while ((c = c.Parent) != null); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
例えば、Form(=this) 上のcontrol の親子関係とか考えずにそのRectangle表現が欲しいならこうする