Posted by webmaster Guido
In Reply to Making a grid with FG/BG setable colors posted by Lee Hallin
You could use a Delphi StringGrid component with 9 rows and 9 columns. Or else, simply use 81 labels (or panels or other components that can display a text).
1. With a StringGrid
Add a stringgrid to the form, with the desired number of rows and columns. If you don't want a fixed row for headers, or a fixed column, set these properties in the Object Inspector.
At design time, set up two arrays that contain the foreground/background colours of each stringgrid cell. At program start, fill the arrays with default colour combinations. For example:
for Row := 0 to ... do begin for Col := 0 to ... do begin aColorFG[Row, Col] := clBlack; aColorBG[Row, Col] := clWhite; end; end;
Add an event-handler that displays each cell of the stringgrid in the desired colour. The colours are obtained from the colour-arrays above.
2. With labels
It's quite a tedious job to manually set up 81 labels at design time. And modifying the design later on to 100 labels would be a nightmare... So, it's best to create the labels at runtime, for example with a procedure called "CreateLabels". For each label that is created, add a pointer to a "component-array".
That way, you can access the labels in your program code with their row/column coordinates, instead of using their names.
Next: write a routine that sets the colours of a stringgrid cell or of a label, based on its coordinates. Example:
procedure SetColours(Row, Col: integer; FG, BG: TColor);
And finally, if you opted for using labels or panels, write a routine that sets the caption (text) of a component based on it's row/column coordinates in the component-array. Example:
procedure SetLabelCaption(Row, Column: integer; S: string);
So, what do you prefer: stringgrid, labels, panels, anything else? Based on your choice, I'll brew some real code :)
Related Articles and Replies