Link to home
Start Free TrialLog in
Avatar of MissManal
MissManal

asked on

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) ?
Avatar of systan
systan
Flag of Philippines image

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

Avatar of jimyX
jimyX

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.
Avatar of MissManal

ASKER

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

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

How i can execute this function onButton1Click (onClick) event?
ASKER CERTIFIED SOLUTION
Avatar of jimyX
jimyX

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial