Recent

Author Topic: StringGrid: Drag a cell to drop at another cell  (Read 8401 times)

asdf

  • Sr. Member
  • ****
  • Posts: 310
StringGrid: Drag a cell to drop at another cell
« on: December 10, 2010, 08:58:14 pm »
What is the technique to do that?
Lazarus 1.2.4 / Win 32 / THAILAND

asdf

  • Sr. Member
  • ****
  • Posts: 310
Re: StringGrid: Drag a cell to drop at another cell
« Reply #1 on: December 11, 2010, 06:06:46 pm »
I got this from internet:

implementation

{$R *.lfm}

{ TForm1 }

var
  SourceCol, SourceRow: integer;

procedure TForm1.FormCreate(Sender: TObject);
begin
  { Set DragMode to Manual, to control entire drag & drop by code }
  SG.DragMode := dmManual;
  { For testing purposes, fill a few cells }
  SG.Cells[2, 2] := 'A';
  SG.Cells[3, 2] := 'B';
  SG.Cells[4, 2] := 'C';
end;

procedure TForm1.ExitMenuItemClick(Sender: TObject);
begin
  application.Terminate;
end;

procedure TForm1.SGMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: integer);
begin
  { Convert mouse coordinates X, Y to
    to StringGrid related col and row numbers }
  SG.MouseToCell(X, Y, SourceCol, SourceRow);
  { Allow dragging only if an acceptable cell was clicked
    (cell beyond the fixed column and row) }
  if (SourceCol > 0) and (SourceRow > 0) then
    { Begin dragging after mouse has moved 4 pixels }
    SG.BeginDrag(False, 4);
end;

procedure TForm1.SGDragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
var
  CurrentCol, CurrentRow: integer;
begin
  SG.MouseToCell(X, Y, CurrentCol, CurrentRow); // convert mouse coord.
  { Accept dragged stuff only if it came from StringGrid
    and the mouse is now over an acceptable region }
  Accept := (Sender = Source) and
            (CurrentCol > 0) and (CurrentRow > 0);
end;

procedure TForm1.SGDragDrop(Sender, Source: TObject; X, Y: Integer);
var
  DestCol, DestRow: Integer;
begin
  SG.MouseToCell(X, Y, DestCol, DestRow); // convert mouse coord.
{ Move contents from source to destination }
  SG.Cells[DestCol, DestRow] := SG.Cells[SourceCol, SourceRow];
  if (SourceCol <> DestCol) or (SourceRow <> DestRow) then
    SG.Cells[SourceCol, SourceRow] := '';
end;

But I lose the ability to right click the mouse to activate the PopUpMenu.

What should be edited?
                         
Lazarus 1.2.4 / Win 32 / THAILAND

fabienwang

  • Sr. Member
  • ****
  • Posts: 449
  • Lazarus is the best
    • My blog
Re: StringGrid: Drag a cell to drop at another cell
« Reply #2 on: December 11, 2010, 06:14:53 pm »
asdf, in the procedure TForm1.SGMouseDown
you shouldn't do SG.BeginDrag if the button was the right click :)

then your popup menu will work
I'm using Arch Linux.
Known for: CPickSniff, OpenGrabby
Contributed to: LazPaint

asdf

  • Sr. Member
  • ****
  • Posts: 310
Re: StringGrid: Drag a cell to drop at another cell
« Reply #3 on: December 11, 2010, 06:50:54 pm »
Quote
procedure TForm1.SGMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: integer);
begin
   SG.MouseToCell(X, Y, SourceCol, SourceRow);
  // if (SourceCol > 0) and (SourceRow > 0) then
  // SG.BeginDrag(False, 4);

end;

Doing as above, cell's content didn't move and it looked like range select. Please help.
Lazarus 1.2.4 / Win 32 / THAILAND

fabienwang

  • Sr. Member
  • ****
  • Posts: 449
  • Lazarus is the best
    • My blog
Re: StringGrid: Drag a cell to drop at another cell
« Reply #4 on: December 11, 2010, 07:06:01 pm »
nope here is what i meant:

Code: [Select]
procedure TForm1.SGMouseDown(Sender:TObject; Button:TMouseButton;
  Shift:TShiftState; X,Y:Integer);
begin
  if Button = mbLeft then
    begin
      SG.MouseToCell(X, Y, SourceCol, SourceRow);
      if (SourceCol > 0) and (SourceRow > 0) then
        SG.BeginDrag(False, 4);
    end;
end;  
I'm using Arch Linux.
Known for: CPickSniff, OpenGrabby
Contributed to: LazPaint

asdf

  • Sr. Member
  • ****
  • Posts: 310
Re: StringGrid: Drag a cell to drop at another cell
« Reply #5 on: December 11, 2010, 07:34:45 pm »
Thank you so much for your assistance  :D .
Lazarus 1.2.4 / Win 32 / THAILAND

 

TinyPortal © 2005-2018