Author: William Gerbert
How to copy/paste TStringGrids cells to/from ClipBoard
Answer:
1 uses
2 Clipbrd;
3
4 //Copy
5
6 procedure TForm1.Button1Click(Sender: TObject);
7 var
8 S: string;
9 GRect: TGridRect;
10 C, R: Integer;
11 begin
12 GRect := StringGrid1.Selection;
13 S := '';
14 for R := GRect.Top to GRect.Bottom do
15 begin
16 for C := GRect.Left to GRect.Right do
17 begin
18 if C = GRect.Right then
19 S := S + (StringGrid1.Cells[C, R])
20 else
21 S := S + StringGrid1.Cells[C, R] + #9;
22 end;
23 S := S + #13#10;
24 end;
25 ClipBoard.AsText := S;
26 end;
27
28 //Paste
29
30 procedure TForm1.Button2Click(Sender: TObject);
31 var
32 Grect: TGridRect;
33 S, CS, F: string;
34 L, R, C: Byte;
35 begin
36 GRect := StringGrid1.Selection;
37 L := GRect.Left;
38 R := GRect.Top;
39 S := ClipBoard.AsText;
40 R := R - 1;
41 while Pos(#13, S) > 0 do
42 begin
43 R := R + 1;
44 C := L - 1;
45 CS := Copy(S, 1, Pos(#13, S));
46 while Pos(#9, CS) > 0 do
47 begin
48 C := C + 1;
49 if (C <= StringGrid1.ColCount - 1) and (R <= StringGrid1.RowCount - 1) then
50 StringGrid1.Cells[C, R] := Copy(CS, 1, Pos(#9, CS) - 1);
51 F := Copy(CS, 1, Pos(#9, CS) - 1);
52 Delete(CS, 1, Pos(#9, CS));
53 end;
54 if (C <= StringGrid1.ColCount - 1) and (R <= StringGrid1.RowCount - 1) then
55 StringGrid1.Cells[C + 1, R] := Copy(CS, 1, Pos(#13, CS) - 1);
56 Delete(S, 1, Pos(#13, S));
57 if Copy(S, 1, 1) = #10 then
58 Delete(S, 1, 1);
59 end;
60 end;
|