Autor Beitrag
hibbert
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1007

WinServer2003, Win XP, Linux
D6 Pers, D05
BeitragVerfasst: Mi 21.03.07 14:28 
Hallo,
ich habe eine einfache Listbox. Den Methoden OnDragDrop und OnDragOver habe ich jeweils folgendes hinzugewiesen:
ausblenden volle Höhe Delphi-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:
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 TListBox(Source).Items.Count-1 >= iTemp 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;

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

Den Code habe ich hier im DF gefunden. Der Funktioniert auch super, sofern ich nur ein Item gleichzeitig verschieben möchte. Markiere ich allerdings mehrere Items, so dreht sich die Reihenfolge um.
Beispiel:
  • Listbox mit dem Items:: 1,2,3,4,5,6,7,8,9,10
  • Ich markiere Items 1,2,3
  • Schiebe diese zwischen Item 5 und 6
  • Neue Reihenfolge: 4,5,3,2,1,6,7,8,9,10

Ich habe bis jetzt noch nicht herrausgefunden woran das liegt. Vllt seh ich mal wieder den Wald vor lauter Bäumen nicht, aber vllt sieht ja wer anders den Wald *g*

_________________
I kunnen väl svara endast ja eller nej
Om i viljen eller nej
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Fr 23.03.07 15:50 
Hallo,

schon eine Lösung gefunden?

Der Code fügt beginnend von oben die selectierten Items mit Insert an gewählte Stelle ein. Mit Insert wird über dem angegebenen Index eingefügt.
Also zuerst die 1, dann darüber die 2 ...

Ich bin aber ein wenig erstaunt das alle 3 Items verschoben werden,
bei meinem Test wurden nur 2 Items verschoben, da beim verschieben SelCount verändert wird.

Der Code Link aus der DP-Code-Library ist dafür besser geeignet.

Kannst auch mal meinen Code alternativ testen, der kann besser mit nicht zusammenhängender Selectierung umgehen:
ausblenden volle Höhe Delphi-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:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
var InsertIndex,i,Count,TempSelCount : Integer;
   sl : TStringList;
begin
  with TListBox(Source) do
    begin
    InsertIndex := ItemAtPos(Point(X,Y),True);
    if InsertIndex = -1 then//Item(s) unterhalb des letzten Item einfügen
      begin
      TempSelCount := SelCount;
      i := 0;
      Count := Items.Count -1;
      while i < Count do
        if Selected[i] then
          begin
          Items.add(Items[i]);
          Items.Delete(i);
          end
          else
            inc(i);//nur wenn nicht delete
      //eingefügte wieder selectieren
      if MultiSelect then
        for i :=  Count - TempSelCount + 1 to Count  do
          Selected[i] := True
        else
          ItemIndex := Count;
      end
      else
        begin//Item(s) innerhalb Items einfügen
        sL := TstringList.Create;
          try
          for i := 0 to Items.Count -1 do
            begin
            //die zu verschiebenden Item(s) auslagern
            if Selected[i] then
              sL.Add(Items[i]);
            //die Anzahl der bis zur Einfügestelle selectierten items
            //vom insertIndex abziehen
            //damit später an der richtigen Stelle eingefügt wird
            if i = InsertIndex then
              dec(InsertIndex,sL.Count);
            end;
          //die zu verschiebenden Items löschen
          for i := Items.Count -1 downto 0 do
            if Selected[i] then
              Items.Delete(i);
          //ausgelagerte Items rückwärts wieder einfügen
          //um eine richtige Reihenfolge zu erreichen
          for i := sL.Count -1 downto 0 do
            Items.Insert(InsertIndex,sL[i]);
          //eingefügte wieder selectieren
          if MultiSelect then
            for i := InsertIndex to InsertIndex + sL.Count -1 do
              Selected[i] := True
            else
              ItemIndex := InsertIndex;
          finally
            sL.Free;
          end;
        end;
    end;
end;

procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
var InsertIndex : Integer;
begin
  if (Source is TListBox) and (Sender = Source) then
    with TListBox(Source) do
      begin
      InsertIndex := ItemAtPos(Point(X,Y),True);
      if InsertIndex <> -1 then//Drop innerhalb Items
        if not (Selected[InsertIndex]) then//nicht auf selectiertes Item
          Accept := True
          else//Drop unterhalb Items
            Accept := False;
      end
      else
        Accept := False;
end;

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
hibbert Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1007

WinServer2003, Win XP, Linux
D6 Pers, D05
BeitragVerfasst: Fr 23.03.07 17:15 
Hi,
nein bisher hatte ich noch keine Lösung gefunden, war auch andersweitig beschäftigt :(

Dein Quellcode funtioniert recht gut, bis auf ein kleinen Fehler: Möchte ich ein Item gaaanz ans Ende ziehen, bekomme ich folgende Fehlermeldung:
Zitat:
Stil des Listenfeldes (ListBox1) muss virtuell sein, damit Count gesetzt werden kann.

Naja kein Problem, den Listenstyle umgestellt.
Allerdings steht dann auch nichts mehr in der Listbox drin, wenn ich Items hinzufügen :?

Ich werd mal sehn, was man da noch rumbasteln kann ;)

danke jedenfalls erstmal. vllt findet ja wer anders auch noch ne akzeptable Lösung

_________________
I kunnen väl svara endast ja eller nej
Om i viljen eller nej
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Fr 23.03.07 19:05 
Hallo,

die Fehlermeldung kenn ich nicht. Der Code ist mit D3 und D2005 getestet, es gab keinen Fehler :(
Werde es auch mal mit D4 Testen, kann ich aber erst am Montag.

Welchen Style hast Du eingestellt und welcher Style war vorher eingestellt?
Ist es lbOwnerDraw... dann musst Du in OnDrawItem das Zeichnen selbst übernehmen.

//Edit: hab mich mal ein bischen umgeschaut, stell mal den Style auf lbVirtual.

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
hibbert Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1007

WinServer2003, Win XP, Linux
D6 Pers, D05
BeitragVerfasst: Sa 24.03.07 11:06 
user profile iconLannes hat folgendes geschrieben:
[...]hab mich mal ein bischen umgeschaut, stell mal den Style auf lbVirtual.

Dann ist die Listbox immer leer wenn ich überhaupt Items hinzufügen möchte.

_________________
I kunnen väl svara endast ja eller nej
Om i viljen eller nej
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Mi 28.03.07 21:33 
Hallo,

hast Du das Problem inzwischen schon beseitigt?, wenn ja, wo war die Ursache?

In welcher Zeile des Codes kommt eigentlich die Fehlermeldung,
und hast Du mal meinen Code in einem neuen Projekt getestet?

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )