이 해결 방법은 FlowLayoutPanel
경우에만 사용됩니다.
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);
}
사용 예 : 그들이 Controls 컬렉션에 나타난 순서대로 컨트롤을 낳는 때문에, 트릭은 위치를 교환해야 컨트롤의 인덱스를 발견하고이를 전환하는 과정이다
private void Button_Click(object sender, EventArgs e)
{
SwapLocations(sender as Control, Direction.Next);
}
"다음"과 "이전"을 어떻게 정의합니까? 탭 순서? 컨테이너에 추가되는 순서? 위치 기반? –
부모의 Controls 컬렉션에있는 순서입니다. 시각적으로,이 경우 흐름 패널에서 컨트롤의 앞이나 뒤에 오는 컨트롤입니다. – Svish