Sometimes you want to change the order of your controls during runtime.
Usually you only can send them all the way to the front (Control.BringToFront) or
send them all the way back (Control.SendToBack).
The following procedure will help you moving a control just one position for or back.
This is especially useful when using vector graphis or similar applications.
The procedure takes the control to be moved as the first parameter.
The direction is the second parameter (True brings the control one step up,
False sends it one step back).
Internally, this procedure works with SendToBack and BringToFront too, however,
it creates an order list first and pushes the other controls as well.
}procedure ChangeControlZOrder(Sender: TObject; MoveUp: Boolean = True);
I, Curr: Integer;
beginif Sender is TControl thenbegin// only components of type TControl and descendends // work
Control := Sender as TControl;
// has no parent, cannot move ;-) if Control.Parent = nilthen// quit
// determine position in z-order
Curr := -1;
for I := 0 to Pred(Control.Parent.ControlCount) doif Control.Parent.Controls[I] = Sender thenbegin
Curr := I;
if Curr < 0 then// position not found, quit
List := TList.Create;
tryif MoveUp thenbeginfor I := Curr + 2 to Pred(Control.Parent.ControlCount) do// load other controls in group
for I := 0 to Pred(List.Count) do// move other controls to front, too
endelsebeginfor I := 0 to Curr - 2 do// load other controls in group
for I := Pred(List.Count) downto 0 do// move other controls to back, too