1. Home
  2. Computing & Technology
  3. Delphi Programming

How to move PageControl's tabs using drag'n'drop

By Zarko Gajic, About.com

Why not enable your users to rearrange tabs in a TPageControl component at run time using drag and drop...
Drop a TPageControl on a form, add several (tab) pages and handle the MouseDown, DragDrop and DragOver events of the TPageControl (PageControl1) component.

~~~~~~~~~~~~~~~~~~~~~~~~~
procedure TMainForm.PageControl1MouseDown(Sender: TObject;
   Button: TMouseButton; Shift: TShiftState; X, Y: Integer) ;
begin
   PageControl1.BeginDrag(False) ;
end;

procedure TMainForm.PageControl1DragDrop(Sender, Source: TObject; X,
   Y: Integer) ;
const
   TCM_GETITEMRECT = $130A;
var
   TabRect: TRect;
   j: Integer;
begin
   if (Sender is TPageControl) then
   for j := 0 to PageControl1.PageCount - 1 do
   begin
     PageControl1.Perform(TCM_GETITEMRECT, j, LParam(@TabRect)) ;
     if PtInRect(TabRect, Point(X, Y)) then
     begin
       if PageControl1.ActivePage.PageIndex <> j then
         PageControl1.ActivePage.PageIndex := j;
       Exit;
     end;
   end;
end;

procedure TMainForm.PageControl1DragOver(Sender, Source: TObject; X,
   Y: Integer; State: TDragState; var Accept: Boolean) ;
begin
   if (Sender is TPageControl) then Accept := True;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~
Need more drag'n'drop related article? Find them here: Handling drag and drop operations in Delphi

Delphi tips navigator:
» How to open a password protected Paradox DB (if you do not know the password)
« How to dynamically change the Page title in ASP.NET

Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Using VCL Components
  5. TPageControl
  6. How to move PageControl's tabs using drag'n'drop

©2009 About.com, a part of The New York Times Company.

All rights reserved.