• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 403
  • Last Modified:

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) ?
0
MissManal
Asked:
MissManal
  • 3
  • 2
1 Solution
 
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
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
jimyXCommented:
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now