Author: Tomas Rutkauskas
All I want to do is to allow the user to drag/ drop panels within a scrollbox to
rearrange the order. They are initially created in alpha order by caption anf
pointers stored in a stringlist. When the user moves one it is moved in the
stringlist, the scrollbox is cleared (note: the panels cannot be freed because they
contain info) and re-parented in stringlist sequence. If you do more than one move,
the resulting sequence of the panels in the scrollbox seems completely random, and
certainly bears no resemblance to the sequence in the stringlist! I have tried
doing this by index up from and downto zero; neither works.
Answer:
All your trouble comes from using the alTop style for the panels. Simply set it to
alNone and size and position the panels in code, using their SetBounds method.
1 procedure TForm1.FormCreate(Sender: TObject);
2 var
3 iCount: integer;
4 APanel: TPanel;
5 y: Integer;
6 begin
7 AStrList := TStringList.Create;
8 y := 0;
9 for iCount := 0 to 4 do
10 begin
11 APanel := TPanel.Create(Self);
12 with APanel do
13 begin
14 Name := 'P' + IntToStr(iCount);
15 Align := alNone;
16 OnMouseDown := PanelMouseDown;
17 OnDragOver := PanelDragOver;
18 OnDragDrop := PanelDragDrop;
19 SetBounds(0, y, scrollbox1.clientwidth, height);
20 Inc(y, height);
21 end;
22 AStrList.AddObject(APanel.Caption, APanel);
23 end;
24 for iCount := 0 to (AStrList.Count - 1) do
25 TPanel(AStrList.Objects[iCount]).Parent := ScrollBox1;
26 end;
27
28 procedure TForm1.PanelDragDrop(Sender, Source: TObject; X, Y: Integer);
29 var
30 iFrom, iTo, iCount: integer;
31 begin
32 iFrom := AStrList.IndexOfObject(TPanel(Sender));
33 iTo := AStrList.IndexOfObject(TPanel(Source));
34 AStrList.Move(iFrom, iTo);
35 y := 0;
36 for iCount := 0 to (AStrList.Count - 1) do
37 with TPanel(AStrList.Objects[iCount]) do
38 begin
39 Top := y;
40 Inc(y, Height);
41 end;
42 end;
|