tags:

views:

837

answers:

2

I am adding custom controls to a FlowLayoutPanel. Each control has a date property. I would like to sort the controls in the flowlayoutpanel based on the date property. I can't presort the controls before I add them because it is possible for the user to add more.

My current thought is when the ControlAdded event for the FlowLayoutPanel is triggered I loop through the controls and use the BringToFront function to order the controls based on the date.

What is the best way to do this?

A: 

BringToFront affects the z-order not the x/y position, I suspect you want to sort the FlowLayoutPanel.Controls collection when someone adds or deletes controls in the panel. Probably use SuspendLayout and ResumeLayout around the sorting code.

Andrew Queisser
+1  A: 

I doubt this is the best but is what I have so far:

        SortedList<DateTime,Control> sl = new SortedList<DateTime,Control>();
        foreach (Control i in mainContent.Controls)
        {
            if (i.GetType().BaseType == typeof(MyBaseType))
            {
                MyBaseType iTyped = (MyBaseType)i;
                sl.Add(iTyped.Date, iTyped);
            }
        }


        foreach (MyBaseType j in sl.Values)
        {
            j.SendToBack();
        }
Maudite