Recent

Author Topic: TStringGrid call copy/paste  (Read 14753 times)

sfeinst

  • Sr. Member
  • ****
  • Posts: 258
TStringGrid call copy/paste
« on: March 09, 2010, 03:45:11 pm »
The default processing of the TStringGrid works just the way I want regarding cut/copy/paste.  What i want to do is give the user a menu otpion to do the work.  How do I pass the key combination to the grid?  So if I create a menu, Copy which has a shortcut CTRL-C, what do I write in the body for the menu onclick to get the TStringGrid to operate as if CTRL-C were pressed on it?

Thanks

mathio

  • Newbie
  • Posts: 6
    • my website
Re: TStringGrid call copy/paste
« Reply #1 on: March 09, 2010, 04:01:41 pm »
This is actually a copy. Is where you choose to save it?  :(
Mathio

sfeinst

  • Sr. Member
  • ****
  • Posts: 258
Re: TStringGrid call copy/paste
« Reply #2 on: March 09, 2010, 04:10:55 pm »
This is actually a copy. Is where you choose to save it?  :(
Sorry, I'm not sure what you are asking.  The CTRL-C on a TStringGrid will copy the selected cells.  I want to add a menu choice to the app that has a copy menu so the user can select the menu instead of pressing CTRL-C (I also want to add cut and paste as well).

I'm trying to find out how to send the CTRL-C to the grid programmatically or to call a method on the grid which mimics what the CTRL-C does.  Grid.CopyToClipboard copies the whole grid (and I don't see a cut or paste).

sfeinst

  • Sr. Member
  • ****
  • Posts: 258
Re: TStringGrid call copy/paste
« Reply #3 on: March 09, 2010, 05:36:08 pm »
What I am doing right now is I subclassed TStringGrid.  Added 3 procedures which call DoCopyToClipboard, DoPasteFromClipboard and DoCutToClipboard.  This works, but I was hoping not to subclass the grid.  Too bad the Do..Clipboard methods are all protected.
« Last Edit: March 09, 2010, 06:16:09 pm by sfeinst »

jesusr

  • Sr. Member
  • ****
  • Posts: 499
Re: TStringGrid call copy/paste
« Reply #4 on: March 09, 2010, 07:15:26 pm »
In r23901 we added an optional parameter to CopyToClipboard if optional parameter is true, it will use grid.selection as range of cells if it's false the behavior is the same as before.

jwdietrich

  • Hero Member
  • *****
  • Posts: 1274
    • formatio reticularis
Re: TStringGrid call copy/paste
« Reply #5 on: August 20, 2011, 03:59:30 pm »
A possible solution for cut and copy is

Code: [Select]
procedure CutorCopyfromGrid(theGrid: TStringGrid; cut: Boolean = False);
const
  kTAB = #9;
  kCRLF = #13#10;
var
  theSelection: TGridRect;
  r, c: integer;
  textfromSelection: AnsiString;
begin
  theSelection := theGrid.Selection;
  textfromSelection := '';
  for r := theSelection.Top to theSelection.Bottom do
  begin
    for c := theSelection.Left to theSelection.Right do
    begin
      textfromSelection := textfromSelection + theGrid.Cells[c,r];
      if cut then
      begin
        theGrid.Cells[c,r] := '';
      end;
      if c < theSelection.Right then
        textfromSelection := textfromSelection + kTAB;
    end;
    if r < theSelection.Bottom then
      textfromSelection := textfromSelection + kCRLF;
  end;
  ClipBoard.AsText := textfromSelection;
end;                         

The original version of this is from http://borland.newsgroups.archived.at/public.delphi.vcl.components.using.win32/200601/0601121293.html., but I simplified the code and adapted it to Lazarus.
« Last Edit: August 20, 2011, 04:13:05 pm by jwdietrich »
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 4.2.0 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

 

TinyPortal © 2005-2018