Recent

Author Topic: Drag and Drop with StringGrid component  (Read 6907 times)

Emmanuel

  • New Member
  • *
  • Posts: 24
Drag and Drop with StringGrid component
« on: October 01, 2013, 08:27:06 pm »
I try to use the drag and drop function with a stringGrid component, without results, have you got any examples to help me  ???

Thank you for your help


BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Drag and Drop with StringGrid component
« Reply #1 on: October 02, 2013, 10:20:21 am »
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Drag and Drop with StringGrid component
« Reply #2 on: October 02, 2013, 02:30:29 pm »
Here's a small example. Start a new project and call the main form mainTest. Adapt the code as follows:
Code: [Select]
unit mainTest;

{$mode objfpc}{$H+}

interface

uses
  SysUtils, Forms, Controls, GridDragDrop;

type

  { TForm1 }

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    sg: TDragDropRows;
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
var
  r,c: integer;
begin
  sg:=TDragDropRows.Create(Self);
  sg.Align:=alClient;
  sg.ColCount:=5;
  for r:= 0 to sg.RowCount-1 do
    for c:= 0 to sg.ColCount-1 do
      sg.Cells[c,r]:=Format('row:%d',[r]);
  sg.Parent:=Self;
  // set other properties here ...
end;

end.
Then create a new unit called GridDragDrop, add it to the project and add this code:
Code: [Select]
unit GridDragDrop;

{$mode objfpc}{$H+}

interface

uses
  Classes, Grids, Controls;

type

{ TDragDropGrid }

 TDragDropRows = class(TCustomStringGrid)
     private
       function RowAtXY(anX, aY: integer): integer;
       procedure GridDragOver(Sender, Source: TObject;
               X,Y: Integer; State: TDragState; var Accept: Boolean);
       procedure GridDragDrop(Sender, Source: TObject; X,Y: Integer);
     public
       constructor Create(AOwner: TComponent); override;
     end;

implementation

{ TDragDropGrid }

function TDragDropRows.RowAtXY(anX, aY: integer): integer;
var
  c: integer;
begin
  MouseToCell(anX, aY, c, Result);
end;

procedure TDragDropRows.GridDragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
var sourceGrid: TStringGrid;
begin
  if (Sender<>Source) or not (Source is TDragDropRows) then Exit;
  sourceGrid:=TStringGrid(Source);
  Accept:=sourceGrid.Row<>RowAtXY(X, Y);
end;

procedure TDragDropRows.GridDragDrop(Sender, Source: TObject; X, Y: Integer);
begin
  MoveColRow(False,(Source as TDragDropRows).Row, RowAtXY(X, Y));
end;

constructor TDragDropRows.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  DragMode:=dmAutomatic;
  OnDragOver:=@GridDragOver;
  OnDragDrop:=@GridDragDrop;
  // set other properties such as Options here
end;

end.

Emmanuel

  • New Member
  • *
  • Posts: 24
Re: Drag and Drop with StringGrid component
« Reply #3 on: October 03, 2013, 06:24:11 pm »
 %) Thank You for your help, I follow your instruction, but I m not sure to know how to use your code, do I have to add a stringGrid on my form ? and after how to link with your code ??? Sorry for these basic questions ;D....
Thank your for your help....
Emmanuel

Ocye

  • Hero Member
  • *****
  • Posts: 518
    • Scrabble3D
Re: Drag and Drop with StringGrid component
« Reply #4 on: October 05, 2013, 10:01:13 am »
Howard's example is for rather advanced users. He illustrates the idea of d&d by creating a component on runtime and setting properties there. To understand it, you need to be able to read Pascal and you should know at least basics about standard components. I suggest to study a tutorial first. Have a look on the left side panel for 'Bookstore'...
Lazarus 1.7 (SVN) FPC 3.0.0

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Drag and Drop with StringGrid component
« Reply #5 on: October 05, 2013, 01:44:01 pm »
Yes, I made it unecessarily complicated if you're not familiar with setting up components for use at runtime.
Try the attached zipped project, made by starting a new Lazarus GUI project, dropping a stringgrid on its main form and adding OnDragDrop and OnDragOver handlers for the grid and an OnCreate handler for the form to populate the grid with string data.

 

TinyPortal © 2005-2018