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 2 procedure LoadResourceIntoGrid(grid: TStringGrid);
3 var4 rs: TResourceStream;
5 numElements: Integer;
6 datarec: TFileData;
7 i: Integer;
8 begin9 rs := TResourceStream.Create(hInstance, 'FILEDATA', RT_RCDATA);
10 try11 numElements := rs.Size div Sizeof(numElements);
12 grid.Perform(WM_SETREDRAW, 0, 0);
13 try14 grid.RowCount := numElements + 1; {assuming a header row}15 {following assumes grids colcount has been set correctly already}16 for i := 1 to numElements do17 begin18 rs.ReadBuffer(datarec, sizeof(datarec));
19 grid.Cells[grid.FixedCols, i] := datarec.col1;
20 grid.Cells[grid.FixedCols + 1, i] := datarec.col2;
21 grid.Cells[grid.FixedCols + 2, i] := datarec.col3;
22 end;
23 finally24 grid.Perform(WM_SETREDRAW, 1, 0);
25 grid.Invalidate;
26 end;
27 finally28 rs.free
29 end;
30 end;