问题。我在同一个表单上有两个 TStringGrid。当我单击第一个表格的单元格(x,y)时,第二个表格的同一单元格(x,y)的背景必须变为红色。由于桌子不同,我该怎么办?
我知道如何使用 table1 上的 OnClick 方法查找单元格,但我不知道如何为 table2 中的单元格着色
问题。我在同一个表单上有两个 TStringGrid。当我单击第一个表格的单元格(x,y)时,第二个表格的同一单元格(x,y)的背景必须变为红色。由于桌子不同,我该怎么办?
我知道如何使用 table1 上的 OnClick 方法查找单元格,但我不知道如何为 table2 中的单元格着色
您必须以某种方式告诉 grid2 您要突出显示哪些单元格。有很多方法可以做到这一点,这取决于你想要做什么。
如果您只想突出显示单击的最后一个单元格,请创建几个表单变量,例如 fx 和 fy 并将它们设置在您的 onclick 事件中并刷新 grid2。然后对网格 2 使用以下 OnDraw 事件。
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;
显然,这可以扩展为您希望记录所有单击的框。另一种方法是使用 objects 属性来告诉 StringGrid2 传递此信息,例如通过将 StringGrid1 分配给 objects 属性(或任何其他对象!)
然后例程变成
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;
当然,这些只是一个起点。
谢谢,我已经实现了我的部分目的:
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;