This solution is rather specific for the FlowLayoutPanel
case. Since it lays out the controls in the order that they appear in the controls collection, the trick is of course to find out the indices of the controls that should swap locations, and switch them:
private enum Direction
{
Next,
Previous
}
private void SwapLocations(Control current, Direction direction)
{
if (current == null)
{
throw new ArgumentNullException("current");
}
// get the parent
Control parent = current.Parent;
// forward or backward?
bool forward = direction == Direction.Next;
// get the next control in the given direction
Control next = parent.GetNextControl(current, forward);
if (next == null)
{
// we get here, at the "end" in the given direction; we want to
// go to the other "end"
next = current;
while (parent.GetNextControl(next, !forward) != null)
{
next = parent.GetNextControl(next, !forward);
}
}
// get the indices for the current and next controls
int nextIndex = parent.Controls.IndexOf(next);
int currentIndex = parent.Controls.IndexOf(current);
// swap the indices
parent.Controls.SetChildIndex(current, nextIndex);
parent.Controls.SetChildIndex(next, currentIndex);
}
Usage example:
private void Button_Click(object sender, EventArgs e)
{
SwapLocations(sender as Control, Direction.Next);
}