2

Problem. I have two TStringGrids on the same Form. When I click on a cell (x, y) of the first Table, the background of the same cell (x, y) of the second table must turn red. How can I do since the tables are different?

I know how to find a cell using the OnClick method on table1, but I don't know how to color a cell in table2

systemgvp
  • 41
  • 7
  • What have you tried so far? What are your issues? Do you know how to change the colour of a grid cell ? Do you know how to find the cell that has been clicked? If so the solution is trivial. – Dsm Jan 01 '21 at 16:09
  • Yes, I know how to find a cell using the OnClick method on table1, but I don't know how to color a cell in table2 – systemgvp Jan 01 '21 at 16:15
  • 1
    In OnClick of table1 call table2.InvalidateCell(x, y); And in table2.OnDrawCell check, if table1.Cell[x, y] is selected. – Delphi Coder Jan 01 '21 at 16:40
  • can you tell me better what to write in ondraw? because to me it colors the whole table. – systemgvp Jan 01 '21 at 18:49
  • Do you want to highlight every cell clicked on just the latest one? – Dsm Jan 01 '21 at 21:53
  • one single cell (x,y) not others – systemgvp Jan 02 '21 at 08:42

2 Answers2

3

You must tell grid2 somehow which cell(s) you want highlighted. There are many ways to do this, depending on what you want to do.

If you just want the last cell clicked highlighted create a couple of form variables, say fx and fy and set them in your onclick event and refresh grid2. Then use the following OnDraw event for grid 2.

procedure TFormAdobeTest.StringGrid2DrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
  if (ACol = fx) and (ACol =fy) then
  begin
    StringGrid2.Canvas.Brush.Color := clRed;
    StringGrid2.Canvas.Rectangle( Rect );
  end
  else
  begin
    StringGrid2.Canvas.Brush.Color := clWhite;
    StringGrid2.Canvas.Rectangle( Rect );
  end;
end;

Obviously this could be extended of you want all clicked boxes recorded. Another way to do this is to instead use the objects property to tell StringGrid2 to pass this information for example by assigning StringGrid1 to the objects property (or any other object!)

an then the routine become

procedure TFormAdobeTest.StringGrid2DrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
  if Assigned( StringGrid2.Objects [ ACol, ARow]) then
  begin
    StringGrid2.Canvas.Brush.Color := clRed;
    StringGrid2.Canvas.Rectangle( Rect );
  end
  else
  begin
    StringGrid2.Canvas.Brush.Color := clWhite;
    StringGrid2.Canvas.Rectangle( Rect );
  end;
end;

These are just a starting point of course.

Dsm
  • 5,870
  • 20
  • 24
1

Thanks, I have achieved part of my purpose with this:

procedure TForm1.StringGrid1Click(Sender: TObject);
begin
  if (StringGrid1.col > 0) and (StringGrid1.row > 0) then
  begin
    cc := StringGrid1.col;
    rr := StringGrid1.row;
  end
  else
  begin
    cc := -1;
    rr := -1;
  end;
  memo1.Lines.Append(cc.ToString+','+rr.ToString);
  StringGrid2.Repaint;
end;

procedure TForm1.StringGrid2DrawCell(Sender: TObject; aCol, aRow: Integer;
  aRect: TRect; aState: TGridDrawState);
begin
  if (ACol = cc) and (aRow = rr) then
  begin
    StringGrid2.Canvas.Brush.Color := clRed;
    StringGrid2.Canvas.Rectangle(aRect);
  end;
end; 

   

enter image description here

systemgvp
  • 41
  • 7