c# - Get Parents of a Control in winforms -
i have panel1 inside panel2 , inside panel3 ... imagine
panel1->panel2->panel3->button1 so how can path string like
string path=\panel1\panel2\panel3\button1 if want parents of button1.
, can using defining class implement iextenderprovider, possible make in design time.
here extension method of parents' names ienumerable<string>:
public static class extensions { public static ienumerable<string> getcontrolpath(this control c) { yield return c.name; if (c.parent != null) { control parent = c.parent; while (parent != null) { yield return parent.name; parent = parent.parent; } } } }
, here property of usercontrol added project make use of it:
public partial class customcontrol : usercontrol { public customcontrol() { initializecomponent(); } public string controlpath { { return string.join(@"\", this.getcontrolpath().reverse()); } } }
after building, drag user control onto form toolbox. sure nest inside other controls pretty good. nested 3 panels , put in innermost panel similar example. here's properties @ design time:

this should applicable class make derives control. iextenderprovider seems irrelevant here.
Comments
Post a Comment