Entwickler-Ecke

Delphi Language (Object-Pascal) / CLX - DragDrop, Initialisierungsfehler?


D. Annies - Fr 30.01.09 11:11
Titel: DragDrop, Initialisierungsfehler?
Hi Delpher,

mit dem folgenden Code ziehe ich per Drag&Drop Items aus einer Listbox in ein (Tnt-)Stringgrid.

Nur nach dem Programmstart und beim ersten Itemauswählen passiert es:
Das erste Item der Listbox wird eingetragen und nicht das ausgewählte.
Sonst ist alles korrekt.

Ich habe es auch in einer minimalen Umgebung ausprobiert, da tritt dieser Fehler nicht auf.

Code:

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:
procedure TForm1.ListBox2DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  if StartDrag then  //Store which row we are on when the dragging started.
  begin
    YMouseCord := Y;           //wichtig::  OI:listbox2.DragMode := dmautomatic;
    StartDrag := False;
  end;
end;

procedure TForm1.ListBox2DragDrop(Sender, Source: TObject; X, Y: Integer);
begin  StartDrag := True; end//Falls wir das Item wieder fallen lassen

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

procedure TForm1.TntStringGrid3DragDrop(Sender, Source: TObject; X, Y: Integer);
var i, n, RowVal, CurrentRow : Integer;
begin
  if Source is TListBox then
  begin
    RowVal := 0;  CurrentRow := 0;
    for i := 0 to TListBox(Source).Items.Count-1 do
    begin
      RowVal := RowVal + TListBox(Source).ItemHeight;  //hier 13
      if YMouseCord <= RowVal then
        begin  CurrentRow := i;  break;  end;
    end;
    n := -1;
    repeat
      inc(n);
    until (n >= tntstringgrid3.FixedCols) and (tntstringgrid3.Cells[n,0] = '');
    TntStringgrid3.cells[n,0] := ListBox2.items[CurrentRow];
    Gridbreite(TntStringgrid3);
    StartDrag := True;
  end;
end;


Wer findet da den Fehler??
Gruß, Detlef


D. Annies - Fr 30.01.09 17:43

Nun, in der Tat ein Initialisierungsfehler:

An geeigneter Stelle im Programm [z.B. nach listbox2.visible := true] musste ein StartDrag := true stehen!

Gruß, Detlef