Autor Beitrag
Inferno
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 76



BeitragVerfasst: Do 10.04.03 13:24 
Hi alle

Wie kann ich per Drag&Drop Elemente in einer ListBox verschieben?
Bei mir will er immer die ListBox selber verschieben.

Danke schon im Voraus

Tschü
biohazard
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 89



BeitragVerfasst: Fr 11.04.03 10:50 
Schau mal hier nach:

ausblenden volle Höhe Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
{ 
 This example shows how to drag&drop within a TListBox. 
 The Demo Program also shows how to implement an autoscroll-feature. 

 Dieses Beispiel zeigt, wie man drag & drop in einer TListBox realisiert. 
 Im Demo-Programm wird auch eine Autoscroll-Funktion gezeigt. 
} 

procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer; 
  State: TDragState; var Accept: Boolean); 
begin 
  Accept := Sender is TListBox; 
end; 

procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer); 
var 
  iTemp: Integer; 
  ptTemp: TPoint; 
  szTemp: string; 
begin 
  { change the x,y coordinates into a TPoint record } 
  ptTemp.x := x; 
  ptTemp.y := y; 

  { Use a while loop instead of a for loop due to items possible being removed 
   from listboxes this prevents an out of bounds exception } 
  iTemp := 0; 
   while iTemp <= TListBox(Source).Items.Count-1 do 
  begin 
    { look for the selected items as these are the ones we wish to move } 
    if TListBox(Source).selected[iTemp] then 
    begin 
      { use a with as to make code easier to read } 
      with Sender as TListBox do 
      begin 
      { need to use a temporary variable as when the item is deleted the 
        indexing will change } 
        szTemp := TListBox(Source).Items[iTemp]; 

        { delete the item that is being dragged  } 
        TListBox(Source).Items.Delete(iTemp); 

      { insert the item into the correct position in the listbox that it 
       was dropped on } 
        Items.Insert(itemAtPos(ptTemp, True), szTemp); 
      end; 
    end; 
    Inc(iTemp); 
  end; 
end;