I have a place in my code where I am dynamically adding controls to a Top-Down arranged FlowLayoutPanel. I need the controls to appear in a certain order so what I am doing everytime is clearing the FlowLayoutPanel.Controls collection and then adding each child control in the order I want them to appear. My code does this:
private void arrangement1()
{
flowLayoutPanel1.Controls.Clear();
flowLayoutPanel1.Controls.Add(control1);
flowLayoutPanel1.Controls.Add(control2);
flowLayoutPanel1.Controls.Add(control3);
}
Most of the time this works great. However, there is one specific control that does not maintain it's position in the control collection when adding other controls after it. For instance in the following code segment:
private void arrangement2()
{
flowLayoutPanel1.Controls.Clear();
flowLayoutPanel1.Controls.Add(control1);
flowLayoutPanel1.Controls.Add(movingControl);
//movingControl current is at index = 1 in Controls.
flowLayoutPanel1.Controls.Add(control2);
//control2 is now at index = 1, movingControl got bumped to index = 2 in Controls.
flowLayoutPanel1.Controls.Add(control3);
//control3 is now at index =2, movingControl got bumped to index = 3 in Controls.
}
This only happens the first time movingControl is added to Controls. If I go back and call arrangement1 and then arrangement2 a second time. The controls will appear the in intended order of:
- control1
- movingControl
- control2
- control3
This seems to be a bug in the code for Controls.Add. Either that or the documentation for .Add's behaviour is incomplete as it doesn't always add to the end of the collection. Does anyone have any insight into why this occurs. The obvious "fix" is to just call:
arrangement2();
arrangement1();
arrangement2();
However, that seems like a very poor solution to some other underlying problem.
Thanks in advance for the help!
EDIT: Note that each of these controls is a member of a custom view class so they persist after the Controls collection is Cleared. However, these controls are not stored in any sort of ordered collection. They are just members of this custom class. The code shown above works correctly as shown. However, in the context of my GUI program it has the described erroneous behaviour. I would post more code if I had any idea what would be helpful but there is a lot of code that touches these peices. This is all of the code that executes for the described action.
What I'm really looking for is what possible scenarios cause a Controls.Add to Insert a control not at the last index of the collection. Specifically after a call to Clear() and with NO Remove() calls.