Does anyone know how to do a copy/cut or paste to and from a stringgrid?
I am sure there is a way. Any help much appreciated - cheers
Paul M
There is no direct way, but here is how you can copy. For cut, you can
clear the cells. For paste, you'll have to parse the text and fill the
cells. By the way, this format can be pasted nicely into Excel.
uses ClipBrd;
procedure CopyGridSelectionToClipBoard(Grid: TStringGrid; Selection:
TGridRect);
const
TAB = Chr(VK_TAB);
CRLF = #13#10;
var
r, c: integer;
S: string;
begin
S := '';
for r := Selection.Top to Selection.Bottom do
begin
for c := Selection.Left to Selection.Right do
begin
S := S + Grid.Cells[c,r];
if c < Selection.Right then
S := S + TAB;
end; // for
if r < Selection.Bottom then
S := S + CRLF;
end;
ClipBoard.SetTextBuf(PChar(S));
end;
EXAMPLE:
CopyGridSelectionToClipBoard(StringGrid1, StringGrid1.Selection);
or
var
selection: TGridRect;
begin
selection := TGridRect(Rect(0, 0, 2, 2));
CopyGridSelectionToClipBoard(StringGrid1, selection);
Thanks mate
But I found a quicker method
StringGrid.InplaceEditor.CutToClipboard
does the job nicely
paul
I can now copy to the clipboard. The only problem with this is that I want
to be able to cut a line of text. How do I know what line/selection is
highlighted? If I know this then I can clear the cells.
Paul
For cut, you can clear the cells that you copy. Here is the function
with an extra flag to clear the copied cells.
CopyGridSelectionToClipBoard(Grid, Grid.Selection); // copy
CopyGridSelectionToClipBoard(Grid, Grid.Selection, True); // cut
procedure CopyGridSelectionToClipBoard(Grid: TStringGrid; Selection:
TGridRect;
ClearCells: Boolean = False);
const
TAB = Chr(VK_TAB);
CRLF = #13#10;
var
r, c: integer;
S: string;
begin
S := '';
for r := Selection.Top to Selection.Bottom do
begin
for c := Selection.Left to Selection.Right do
begin
S := S + Grid.Cells[c,r];
if ClearCells then
begin
Grid.Cells[c,r] := '';
end;
I should have mentioned that to begin with, but I assumed you were
interested in a grid area.
"paul mayer" <pma...@kentec.co.uk> wrote in message
news:1f9pdhvxs0bjs$.1xpcgw92eca1q$.dlg@40tude.net...