I have Panel1
inside this Panel2
and inside this Panel3
... So imagine like
Panel1->Panel2->Panel3->button1
So How can get a path string like
string path=\Panel1\Panel2\Panel3\button1
if I want to get all Parents of button1.
And Can I do that using with defining a class which implement IExtenderProvider, so is it possible to make it in design time.
Here is an extension method to get all of the parents' names as an 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;
}
}
}
}
And here is a property of a UserControl that I added to the Project that will make use of it:
public partial class CustomControl : UserControl
{
public CustomControl()
{
InitializeComponent();
}
public string ControlPath
{
get
{
return string.Join(@"\", this.GetControlPath().Reverse());
}
}
}
After building, drag the user control onto the Form from the toolbox. Be sure to nest it inside other controls pretty good. I nested 3 panels and put it in the innermost panel similar to your example. Here's what the properties look like at Design time:
This should be applicable to any class you make that derives from Control
. IExtenderProvider
seems irrelevant here.