locked
Sorting controls inside FlowLayoutPanel RRS feed

  • Question

  • Hi,

    I have user controls that are added (and sometimes removed) to a FlowLayoutPanel dynamically at runtime.
    The FlowLayoutPanel should always display these user controls sorted (sorting based on one of their properties).
    Is there an efficient way to do this?

    For now I'm holding some SortedList and when a new user control is added I get the sort order from the SortedList, add another user control to the panel and iterate through all the controls in the panel setting their correct property values (all user controls are from the same types).

    Thursday, March 8, 2007 5:51 PM

Answers

  • I haven't tested this, but my best guess is that the order of controls going into the flowlayoutpanel is the order they controls are in the FlowLayoutPanel.Controls collection. If you change the order of the Controls collection then it should change the order they are displayed.

    That would only really be practical if you only set that property you are sorting on at construction time - otherwise you have to take the control out of the panel and then add it back in again. This would probably cause some nasty flicker problems.

    Another option is to create your own layout engine (derive it from the LayoutEngine class). Then you can attach that to the flowlayoutpanel control (via the LayoutEngine property of the panel). your layout engine can order the controls any way you like.

    Regards

    Jero

    Friday, March 9, 2007 3:19 AM

All replies

  • Anyone?

    (Sorry for the multiple posts (there were some posting problems today)
    Thursday, March 8, 2007 9:38 PM
  • I haven't tested this, but my best guess is that the order of controls going into the flowlayoutpanel is the order they controls are in the FlowLayoutPanel.Controls collection. If you change the order of the Controls collection then it should change the order they are displayed.

    That would only really be practical if you only set that property you are sorting on at construction time - otherwise you have to take the control out of the panel and then add it back in again. This would probably cause some nasty flicker problems.

    Another option is to create your own layout engine (derive it from the LayoutEngine class). Then you can attach that to the flowlayoutpanel control (via the LayoutEngine property of the panel). your layout engine can order the controls any way you like.

    Regards

    Jero

    Friday, March 9, 2007 3:19 AM
  • Thanks.
    I'll go with my approach of adding\removing controls with no real values to have the required number of controls, hold sorted list and then fill the existing controls based on the order in the sorted list.
    Friday, March 9, 2007 12:46 PM
  • I tested it - and it works.

    Here is a code sample which I put in a context menu "move up" of the controls which I put on a FlowLayoutPanel.

    public void ItemUpClick(object sender, EventArgs e)
            {
                int iCurrentPosition=this.Parent.Controls.GetChildIndex(this);
                this.Parent.Controls.SetChildIndex(this,iCurrentPosition-1);
            }

    Best regards,

    Michal

    • Proposed as answer by Michal Kudra Wednesday, October 31, 2012 6:44 PM
    Wednesday, October 31, 2012 6:43 PM