Thema: Delphi Drag and drop

Einzelnen Beitrag anzeigen

Hansa

Registriert seit: 9. Jun 2002
Ort: Saarland
7.554 Beiträge
 
Delphi 8 Professional
 
#10

Re: Drag and drop

  Alt 13. Feb 2004, 18:39
Scheint keinen mehr zu interessieren.

Ich habe das ganze jetzt mal statt mit einer Listbox mit einem StringGrid versucht:

Delphi-Quellcode:
unit StrTestUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, StdCtrls;

type
  TStringGridExt = class(TStringGrid)
  protected
    procedure InsertRow(ARow: Longint);
    procedure DeleteRow(ARow: Longint);
  end;

type
  TForm2 = class(TForm)
    StringGrid1: TStringGrid;
    Button1: TButton;
    Button2: TButton;
    StringGrid2: TStringGrid;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure StringGrid1DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure StringGrid1DragDrop(Sender, Source: TObject; X, Y: Integer);
    procedure StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    {...}
  public
    {...}
  end;

var
  Form2: TForm2;

implementation

{$R *.DFM}

procedure TStringGridExt.InsertRow(ARow: Longint);
var
  TempRow: Integer;
begin
  TempRow := Row; // Zeile zwischenspeichern
  while ARow < FixedRows do
    ARow := ARow + 1;
  RowCount := RowCount + 1;
  MoveRow(RowCount - 1, ARow);
  Row := TempRow;
  Rows[Row].Clear;
end;

procedure TStringGridExt.DeleteRow(ARow: Longint);
var
  GemRow: Integer;
begin
  GemRow := Row;
  if RowCount > FixedRows + 1 then
    inherited DeleteRow(ARow)
  else
    Rows[ARow].Clear;
  if GemRow < RowCount then Row := GemRow;
end;

procedure TForm2.Button1Click(Sender: TObject);
begin
  TStringGridExt(StringGrid1).InsertRow(1);
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
  TStringGridExt(StringGrid1).DeleteRow(1);
end;

procedure TForm2.StringGrid1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  Accept := true; //Source is TStringGridExt;
end;

procedure TForm2.StringGrid1DragDrop(Sender, Source: TObject; X,
  Y: Integer);
var
  lbSource,lbSender : TStringGridExt;
begin
  lbSource := Source as TStringGridExt;
  lbSender := Sender as TStringGridExt;
  lbsender.InsertRow (lbSender.Row);
  lbSource.DeleteRow (lbSource.Row);
end;

procedure TForm2.StringGrid1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if button = mbLeft then with Sender as TStringGridExt do begin
    if ItemAtPos (Point (x,y),true) >= 0 then
      BeginDrag (false);
  end;
end;

end.
Das StringGridExt enthält 2 Prozeduren, um Zeilen zu löschen / einzufügen. Ansonsten ist es ähnlich, wie bei der Lsitbox. Nur die MouseDown Prozedur wird nicht compiliert, da es kein ItemAtPos im StringGrid gibt. Nur, was soll ich statt dessen verwenden ??
Gruß
Hansa
  Mit Zitat antworten Zitat