Is there an Easier way to Get a 3 deep Panel Control from a Form in order to add a new Control to it programmatically?
- by Mark Sweetman
I have a VB Windows Program Created by someone else, it was programmed so that anyone could Add to the functionality of the program through the use of Class Libraries, the Program calls them (ie... the Class Libraries, DLL files) Plugins, The Plugin I am creating is a C# Class Library. ie.. .dll
This specific Plugin Im working on Adds a Simple DateTime Clock Function in the form of a Label and inserts it into a Panel that is 3 Deep. The Code I have I have tested and it works. My Question is this: Is there a better way to do it? for instance I use Controls.Find 3 different times, each time I know what Panel I am looking for and there will only be a single Panel added to the Control[] array. So again Im doing a foreach on an array that only holds a single element 3 different times.
Now like I said the code works and does as I expected it to. It just seems overly redudant, and Im wondering if there could be a performance issue.
here is the code:
foreach (Control p0 in mDesigner.Controls)
if (p0.Name == "Panel1")
{
Control panel1 = (Control)p0;
Control[] controls = panel1.Controls.Find("Panel2", true);
foreach (Control p1 in controls)
if (p1.Name == "Panel2")
{
Control panel2 = (Control)p1;
Control[] controls1 = panel2.Controls.Find("Panel3", true);
foreach(Control p2 in controls1)
if (p2.Name == "Panel3")
{
Control panel3 = (Control)p2;
panel3.Controls.Add(clock);
}
}
}