Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Copy & Paste in a StringGrid

2,510 views
Skip to first unread message

paul mayer

unread,
Jan 12, 2006, 9:59:01 AM1/12/06
to
Hi

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

dd

unread,
Jan 12, 2006, 10:16:50 AM1/12/06
to

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);


paul mayer

unread,
Jan 12, 2006, 11:08:43 AM1/12/06
to

>
> 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
>


Thanks mate

But I found a quicker method

StringGrid.InplaceEditor.CutToClipboard

does the job nicely

paul

paul mayer

unread,
Jan 12, 2006, 10:50:26 AM1/12/06
to
Thanks dd

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

dd

unread,
Jan 12, 2006, 11:02:05 AM1/12/06
to

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;

dd

unread,
Jan 12, 2006, 11:19:51 AM1/12/06
to

I should have mentioned that to begin with, but I assumed you were
interested in a grid area.

Mark Williams

unread,
Jan 12, 2006, 12:03:13 PM1/12/06
to
I you want to right click and then copy/paste menus you can get the cell
under the mouse by TStringGrid's MouseToCell method. The rest is
straightforward.

"paul mayer" <pma...@kentec.co.uk> wrote in message
news:1f9pdhvxs0bjs$.1xpcgw92eca1q$.dlg@40tude.net...

0 new messages