Iterating through controls on a Windows Form
Posted
by icemanind
on Stack Overflow
See other posts from Stack Overflow
or by icemanind
Published on 2010-05-17T01:37:02Z
Indexed on
2010/05/17
1:40 UTC
Read the original article
Hit count: 390
I seem to have some weird issue going on that I am sure will turn out to be a simple thing. I have a Windows Form and on the form I have 1 panel called MainPanel and inside MainPanel, I got another panel with a button inside and a label that is inside MainPanel, but not in the second panel. 2 controls. What I am trying to do is copy all the controls inside MainPanel over to another panel object. I am using the following C# code to do this:
GUIPanel gp = new GUIPanel();
foreach (System.Windows.Forms.Control ctrl in gp.Controls["MainPanel"].Controls)
{
m_OptionsControl.Controls.Add(ctrl);
}
When I run this code, it copies over the panel with the button, but not the label. What's even more odd is when I set a breakpoint and run it through the debugger, and I type "?gp.Controls["MainPanel"].Controls.Count" in the immediate window, it returns 2, just like it should. However, when stepping through the code, it only executes the foreach loop once. What am I missing here?
© Stack Overflow or respective owner