Author: Tomas Rutkauskas
I am trying to work out how to store approximately 1000 lines of text (3 columns,
30 chars each) inside an application. I want to read the values and then display
them in a TStringGrid and do some further manipulation.
Answer:
If you want to read the data into a stringgrid a way to do that without building a
class around the resource data would be this. You start by placing the data into a
file and the file into a resource as detailed in Tip Number 1004. Loading this data
into a TStringGrid would work like this:
1 procedure LoadResourceIntoGrid(grid: TStringGrid);
2 var3 rs: TResourceStream;
4 numElements: Integer;
5 datarec: TFileData;
6 i: Integer;
7 begin8 rs := TResourceStream.Create(hInstance, 'FILEDATA', RT_RCDATA);
9 try10 numElements := rs.Size div Sizeof(numElements);
11 grid.Perform(WM_SETREDRAW, 0, 0);
12 try13 grid.RowCount := numElements + 1; {assuming a header row}14 {following assumes grids colcount has been set correctly already}15 for i := 1 to numElements do16 begin17 rs.ReadBuffer(datarec, sizeof(datarec));
18 grid.Cells[grid.FixedCols, i] := datarec.col1;
19 grid.Cells[grid.FixedCols + 1, i] := datarec.col2;
20 grid.Cells[grid.FixedCols + 2, i] := datarec.col3;
21 end;
22 finally23 grid.Perform(WM_SETREDRAW, 1, 0);
24 grid.Invalidate;
25 end;
26 finally27 rs.free
28 end;
29 end;