Lazarus

Free Pascal => General => Topic started by: satyendra.shukla on January 21, 2020, 12:38:54 pm

Title: Dragging IMG and Cell Data from nth Cell of TStringGrid to another TStringgrid
Post by: satyendra.shukla on January 21, 2020, 12:38:54 pm
I have created TStringGrid based grid with all cells containing specific images. I can drag and drop an image (drawn over specific cell) to another cell...Hence, the destination cell shows the dropped image..

Now, I want to drag the cell's specific text content and its image to another stringgrid cell (provided is is blank and in same row) in same form... How to achieve it.. Any sample code...

Like sg1.cell[1,2] image and content can be dropped in sg2.cell[1,2] or sg2.cell[2,2] etc..

I am not using TDRAWGrird as I am also storing and managing data in Grid-Cells.

Any help please...
Title: Re: Dragging IMG and Cell Data from nth Cell of TStringGrid to another TStringgrid
Post by: howardpc on January 21, 2020, 01:46:21 pm
The following should get you started.
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, Forms, Controls, Grids;
  9.  
  10. type
  11.  
  12.   TForm1 = class(TForm)
  13.     grid: TStringGrid;
  14.     procedure FormCreate(Sender: TObject);
  15.     procedure gridDragOver(Sender, Source: TObject; X, Y: Integer;
  16.       State: TDragState; var Accept: Boolean);
  17.   private
  18.     startCell: TPoint;
  19.     textToMove: String;
  20.   public
  21.  
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. uses
  30.   SysUtils;
  31.  
  32. {$R *.lfm}
  33.  
  34. procedure TForm1.FormCreate(Sender: TObject);
  35. var
  36.   r, c: Integer;
  37. begin
  38.   grid.DragMode := dmAutomatic;
  39.   grid.Options := grid.Options + [goEditing];
  40.   for r := 0 to grid.RowCount-3 do
  41.     for c := 0 to grid.ColCount-3 do
  42.       grid.Cells[c, r] := Format('c:%d, r:%d',[c, r]);
  43. end;
  44.  
  45. procedure TForm1.gridDragOver(Sender, Source: TObject; X, Y: Integer;
  46.   State: TDragState; var Accept: Boolean);
  47. var
  48.   r, c: Longint;
  49.   g: TStringGrid absolute Sender;
  50. begin
  51.   if not (Sender is TStringGrid) then
  52.     Exit;
  53.   g.MouseToCell(X, Y, c, r);
  54.   case State of
  55.     dsDragEnter: begin
  56.                    startCell.Create(c, r);
  57.                    textToMove := g.Cells[c, r];
  58.                  end;
  59.     dsDragLeave: begin
  60.                    Accept := ((c <> startCell.X) or (r <> startCell.Y)) and (g.Cells[c, r] = '');
  61.                    if Accept then
  62.                      begin
  63.                        g.Cells[c, r] := textToMove;
  64.                        g.Cells[startCell.X, startCell.Y] := '';
  65.                      end;
  66.                  end;
  67.     dsDragMove: Accept := ((c <> startCell.X) or (r <> startCell.Y)) and (g.Cells[c, r] = '');
  68.   end;
  69. end;
  70.  
  71. end.
To have a drag cursor showing the text of the starting cell is somewhat more involved.
Title: Re: Dragging IMG and Cell Data from nth Cell of TStringGrid to another TStringgrid
Post by: satyendra.shukla on January 21, 2020, 02:27:08 pm
The following should get you started.
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, Forms, Controls, Grids;
  9.  
  10. type
  11.  
  12.   TForm1 = class(TForm)
  13.     grid: TStringGrid;
  14.     procedure FormCreate(Sender: TObject);
  15.     procedure gridDragOver(Sender, Source: TObject; X, Y: Integer;
  16.       State: TDragState; var Accept: Boolean);
  17.   private
  18.     startCell: TPoint;
  19.     textToMove: String;
  20.   public
  21.  
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. uses
  30.   SysUtils;
  31.  
  32. {$R *.lfm}
  33.  
  34. procedure TForm1.FormCreate(Sender: TObject);
  35. var
  36.   r, c: Integer;
  37. begin
  38.   grid.DragMode := dmAutomatic;
  39.   grid.Options := grid.Options + [goEditing];
  40.   for r := 0 to grid.RowCount-3 do
  41.     for c := 0 to grid.ColCount-3 do
  42.       grid.Cells[c, r] := Format('c:%d, r:%d',[c, r]);
  43. end;
  44.  
  45. procedure TForm1.gridDragOver(Sender, Source: TObject; X, Y: Integer;
  46.   State: TDragState; var Accept: Boolean);
  47. var
  48.   r, c: Longint;
  49.   g: TStringGrid absolute Sender;
  50. begin
  51.   if not (Sender is TStringGrid) then
  52.     Exit;
  53.   g.MouseToCell(X, Y, c, r);
  54.   case State of
  55.     dsDragEnter: begin
  56.                    startCell.Create(c, r);
  57.                    textToMove := g.Cells[c, r];
  58.                  end;
  59.     dsDragLeave: begin
  60.                    Accept := ((c <> startCell.X) or (r <> startCell.Y)) and (g.Cells[c, r] = '');
  61.                    if Accept then
  62.                      begin
  63.                        g.Cells[c, r] := textToMove;
  64.                        g.Cells[startCell.X, startCell.Y] := '';
  65.                      end;
  66.                  end;
  67.     dsDragMove: Accept := ((c <> startCell.X) or (r <> startCell.Y)) and (g.Cells[c, r] = '');
  68.   end;
  69. end;
  70.  
  71. end.
To have a drag cursor showing the text of the starting cell is somewhat more involved.

Good to see the TPoint. I will try and let you know about it.
Title: Re: Dragging IMG and Cell Data from nth Cell of TStringGrid to another TStringgrid
Post by: satyendra.shukla on January 21, 2020, 03:13:04 pm
This is specific to its own TStringgrid. did not work out. Can you share the zipped working example. I want to drag the Content of 1 cell in Stringgrid1 to another Cell in Stringgrid2..
Title: Re: Dragging IMG and Cell Data from nth Cell of TStringGrid to another TStringgrid
Post by: howardpc on January 21, 2020, 04:14:11 pm
Try the attached project.
Title: Re: Dragging IMG and Cell Data from nth Cell of TStringGrid to another TStringgrid
Post by: wp on January 21, 2020, 04:20:27 pm
Somebody advised me how to implement this feature for the TvPlanIt (https://wiki.freepascal.org/Turbopower_Visual_PlanIt) components. Unfortunately I cannot find the message ATM. But you can study the result in these TvPlanIt units:

- VpBaseDS (https://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/components/tvplanit/source/vpbaseds.pas) --> TVpEventDragObject
- VpWeekView (https://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/components/tvplanit/source/vpweekview.pas) --> TVpWeekView.DoStartDrag, .DragDrop, .DragOver
- VpMonthView (https://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/components/tvplanit/source/vpmonthview.pas) --> TVpMonthView.DoStartDrag, .DragDrop, .DragOver
- VpDayView (https://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/components/tvplanit/source/vpdayview.pas) --> TVpDayView.DoStartDrag, .DragDrop, .DragOver
Title: Re: Dragging IMG and Cell Data from nth Cell of TStringGrid to another TStringgrid
Post by: howardpc on January 21, 2020, 04:55:38 pm
I re-read your first post, because I missed that you wanted to drag between grids, and that dropping should only happen between rows of the same index.
This means your Accept parameter should be
Code: Pascal  [Select][+][-]
  1. Accept := not g.Equals(originatingGrid) and ((g.Cells[c, r] = '') and (r = startCell.Y));
and not as in the project I attached earlier.
Title: Re: Dragging IMG and Cell Data from nth Cell of TStringGrid to another TStringgrid
Post by: satyendra.shukla on January 22, 2020, 06:32:10 am
Fantastic.. This is best example. This way, we can make TStringgrid much better like smartly pushing specific cells to other Tstringgrids on demand...
TinyPortal © 2005-2018