locked
Rearrange usercontrol in flowlayoutpanel RRS feed

  • Question

  • I would like to design a usercontrol that could go Up and Down in flowlayoutpanel; i.e. without using the mouse drag and drop function, how to make the label going up by clicking UP button and going down by click DOWN button
    A UP and DOWN buttons and a other controls that built the usercontrol.

    but since the flowlayoutpanel is placed in form1, how could I use the flowlayoutpanel.Controls.SetChildIndex method in the usercontrol ????

    Wednesday, November 4, 2009 9:57 AM

Answers

  • Hi,

    You can do this by changing index of a control in Controls collection of FlowLayoutPanel.

    Here is a sample.

        public partial class LabelUpDown : UserControl
        {
            public LabelUpDown()
            {
                InitializeComponent();
            }
    
            private void btnUp_Click(object sender, EventArgs e)
            {
                MoveControlInFlowLayoutPanel(false);
            }
            private void MoveControlInFlowLayoutPanel(bool forward)
            {
                if (this.Parent is FlowLayoutPanel)
                {
                    int currentIndex = ((FlowLayoutPanel)this.Parent).Controls.IndexOf(this);
                    ControlCollection cc = ((FlowLayoutPanel)this.Parent).Controls;
    
                    Control previous = null;
                    if (currentIndex - 1 > -1)
                    {
                        previous = cc[currentIndex - 1];
                    }
                    Control next = null;
                    if (currentIndex + 1 < cc.Count)
                    {
                        next = cc[currentIndex + 1];
                    }
                    if (forward)
                    {
                        if (next != null)
                        {
                            cc.SetChildIndex(next, currentIndex);
                            cc.SetChildIndex(this, currentIndex + 1);
                        }
                    }
                    else
                    {
                        if (previous != null)
                        {
                            cc.SetChildIndex(previous, currentIndex);
                            cc.SetChildIndex(this, currentIndex - 1);
                        }
                    }
    
    
    
                }
            }
            private void btnDown_Click(object sender, EventArgs e)
            {
                MoveControlInFlowLayoutPanel(true);
            }
        }
    • Marked as answer by Nise1990 Wednesday, November 4, 2009 11:51 AM
    Wednesday, November 4, 2009 11:22 AM