Mega Code Archive

 
Categories / Delphi / Ide Indy
 

How to change the Z order of you controls during runtime

Title: How to change the Z-order of you controls during runtime procedure ChangeControlZOrder(Sender: TObject; MoveUp: Boolean = True); var I, Curr: Integer; Control: TControl; List: TList; begin if Sender is TControl then begin // only components of type TControl and descendends // work Control := Sender as TControl; // has no parent, cannot move ;-) if Control.Parent = nil then // quit Exit; // determine position in z-order Curr := -1; for I := 0 to Pred(Control.Parent.ControlCount) do if Control.Parent.Controls[I] = Sender then begin Curr := I; Break; end; if Curr 0 then // position not found, quit Exit; List := TList.Create; try if MoveUp then begin for I := Curr + 2 to Pred(Control.Parent.ControlCount) do // load other controls in group List.Add(Control.Parent.Controls[I]); Control.BringToFront; for I := 0 to Pred(List.Count) do // move other controls to front, too TControl(List[I]).BringToFront; end else begin for I := 0 to Curr - 2 do // load other controls in group List.Add(Control.Parent.Controls[I]); Control.SendToBack; for I := Pred(List.Count) downto 0 do // move other controls to back, too TControl(List[I]).SendToBack; end; finally List.Free; end; end; end;