Recent

Author Topic: Dragging IMG and Cell Data from nth Cell of TStringGrid to another TStringgrid  (Read 963 times)

satyendra.shukla

  • New member
  • *
  • Posts: 5
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...

howardpc

  • Hero Member
  • *****
  • Posts: 3552
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.

satyendra.shukla

  • New member
  • *
  • Posts: 5
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.

satyendra.shukla

  • New member
  • *
  • Posts: 5
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..

howardpc

  • Hero Member
  • *****
  • Posts: 3552
Try the attached project.

wp

  • Hero Member
  • *****
  • Posts: 7636
Somebody advised me how to implement this feature for the TvPlanIt components. Unfortunately I cannot find the message ATM. But you can study the result in these TvPlanIt units:

- VpBaseDS --> TVpEventDragObject
- VpWeekView --> TVpWeekView.DoStartDrag, .DragDrop, .DragOver
- VpMonthView --> TVpMonthView.DoStartDrag, .DragDrop, .DragOver
- VpDayView --> TVpDayView.DoStartDrag, .DragDrop, .DragOver
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

howardpc

  • Hero Member
  • *****
  • Posts: 3552
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.

satyendra.shukla

  • New member
  • *
  • Posts: 5
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