2

I'm trying to reverse the order of Controls in a FlowLayoutPanel.

I tried converting the ControlCollection to an array and then reversed that and cleared the ControlCollection and then readded the Controls. But this doesn't seem to have the planned effect.

Here's the code I use:

private static void ReverseLayout(Control control, bool suspend = true) {
     if (suspend) control.SuspendLayout();
     Control[] newCC = new Control[control.Controls.Count];
     control.Controls.CopyTo(newCC, 0);
     Array.Reverse(newCC);
     control.Controls.Clear();
     //control.Controls.AddRange(newCC);
     for (int i = 0; i < newCC.Length; i++) {
        newCC[i].Location = new System.Drawing.Point(); // maybe? no :\
        newCC[i].TabIndex = i; // maybe? no :\
        control.Controls.Add(newCC[i]);
     }
     if (suspend) control.ResumeLayout(false);
  }
svick
  • 236,525
  • 50
  • 385
  • 514
globalenemy
  • 109
  • 2
  • 11
  • According to the [docs](https://msdn.microsoft.com/en-us/system.windows.forms.flowlayoutpanel(v=vs.90)) there is a property with the following description "Gets or sets a value indicating the flow direction of the FlowLayoutPanel control." Unfortunately the docs are broken for me and don't display the name. I would guess its called FlowDirection and the values are LeftToRight, etc.. but play around with intellisense to see if it gives insight as to proper values – Gordon Allocman Mar 10 '16 at 20:44
  • This only changes where objects that are added afterwards will be shown in the panel. objects that are allready there wont be touched. – globalenemy Mar 10 '16 at 21:03

1 Answers1

1

Your code seems more complicated than it needs to be. Try putting the controls in a List<Control> and then call reverse on it, put the collection back:

int firstTabIndex = flp.Controls[0].TabIndex;
List<Control> controls = flp.Controls.Cast<Control>().ToList();
flp.Controls.Clear();
controls.Reverse();
flp.Controls.AddRange(controls.ToArray());

For the TabIndex property, you would have to reapply the value:

for (int i = 0; i < flp.Controls.Count; ++i) {
  flp.Controls[i].TabIndex = firstTabIndex + i;
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • Where did you get 'Cast().ToList()'? ControlCollection doesn't have such a definition – globalenemy Mar 10 '16 at 20:57
  • _using Linq;_ but as far as I can tell this doesn't change the behavior of the FlowLayoutPanel that seems to have lost its capability to properly arrange the re-added controls – Steve Mar 10 '16 at 21:05
  • @globalenemy What version of .net are you using? Cast is an extension of the Enumerable class, requires a reference to the System.Linq. – LarsTech Mar 10 '16 at 21:05
  • @globalenemy Doesn't work how? I just updated it in regards to the TabIndex issue. – LarsTech Mar 10 '16 at 21:14
  • still doesn't do the job. btw. your code is now exaclty the same just that you are having a lst instead of an array. wich you still convert to an array. so much for complicated code. but the step with getting the tabindex is good. i missed that myself. (I don't need the tabindex too be changed btw. that was only a try by myself) – globalenemy Mar 10 '16 at 21:22
  • how: It does not reverse the displayed controls. they remain at position. – globalenemy Mar 10 '16 at 21:31
  • @globalenemy The controls in my FlowLayoutPanel are all reversing with this posted code. – LarsTech Mar 10 '16 at 21:56
  • @globalenemy `control.ResumeLayout(true);` – LarsTech Mar 10 '16 at 22:21