HELP: StringGrid Questions

How I know the color of a cell (let say i need to know the color of cell[3,2] or i have a loop that read the color of each cell) ?
MissManalAsked:
Who is Participating?
 
jimyXConnect With a Mentor Commented:
You can execute this function by calling "StringGrid1.Repaint".

To show you what I meant by using the array:
var
  RC:array of array of TColor;

procedure TForm1.FormCreate(Sender: TObject);
var
  i, j:integer;
begin
  setlength(RC,StringGrid1.ColCount,StringGrid1.RowCount);
  for i:= 0 to StringGrid1.ColCount-1 do
    for j:=0 to StringGrid1.RowCount-1 do
      RC[i,j] := ClWhite;  // or whatever the default color is
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  StringGrid1.Canvas.Brush.Color := RC[ACol,ARow];
  StringGrid1.Canvas.FillRect(Rect);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  RC[3,2] := ClRed;
  StringGrid1.Repaint;
end;

Open in new window

0
 
systanCommented:
http://www.festra.com/wwwboard/messages/12892.html
var ccolor : tcolor;
...
for Col := 0 to Grid.ColCount - 1 do begin
    for Row := 0 to Grid.RowCount - 1 do begin
      tcolor := FG[3, 2];
     // BG[Col, Row] := clWhite;
    end;
  end;

Open in new window

0
 
jimyXCommented:
The TStringGrid can have the colors changed at the event OnDrawCell where you test if the current drawing cell is the cell that you are targeting then make the color changes.
var
  R :integer=-1;
  C :integer=-1;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if (ARow=R) and (ACol=C) then
    begin
      StringGrid1.Canvas.Brush.Color := clRed;
      StringGrid1.Canvas.FillRect(Rect);
    end;
end;

procedure TForm1.StringGrid1Click(Sender: TObject);
begin
  C := StringGrid1.Col;
  R := StringGrid1.Row;
  StringGrid1.Repaint
end;

Open in new window


To know the color of a particular cell:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
// the cells' color have been drawn here first then
  if (ARow=3) and (ACol=3) then
    begin
      ShowMessage(ColorToString(StringGrid1.Canvas.Brush.Color));
    end;
end;

Open in new window


Or to get the color of the selected cell:
procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(ColorToString(StringGrid1.Canvas.Brush.Color));
end;

Open in new window


If you want the TStringGrid to maintain the format you apply or if you need to keep track of the cells, then you can declare a two dimensions global array (which has items that are equivalent to the TStringGrid cells) that stores the state of all the cells and OnDrawCell you just detect the state of the array and apply on the respective cell on the TStringGrid.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
MissManalAuthor Commented:
jimyX if i want show the color of cell[3,2] without selecting the cell on the mouse.... what should i do?

0
 
jimyXCommented:
By using OnDrawCell?
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if (ARow=2) and (ACol=3) then
    begin
      ShowMessage(ColorToString(StringGrid1.Canvas.Brush.Color));
    end;
end;

Open in new window

0
 
MissManalAuthor Commented:
How i can execute this function onButton1Click (onClick) event?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.