【发布时间】:2016-10-13 16:23:36
【问题描述】:
我正在尝试制作一个应用程序,当我单击它们时,TStringGrid 的单元格会改变颜色。每次我点击一个单元格时,它应该切换到下一个颜色并保持该颜色,直到我再次点击该单元格,按顺序:
白色 ==> 红色 ==> 橙色 ==> 绿色 ==> 白色(如红绿灯)。
我遇到的错误有点难以解释,但我会尝试。
应用程序运行,但是当我单击一个单元格然后单击另一个单元格时,有时我单击的第一个单元格会改变颜色,但第二个不会。其他时候,两个单元格都会改变颜色。其他时候,两个单元格都只是重置为白色状态。
type
TForm1 = class(TForm)
StringGrid: TStringGrid;
procedure StringGridDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
private
arrState: array[1..4, 1..4] of Integer;
end;
procedure TForm1.StringGridDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
iRow, iCol: Integer;
arrk: array[1..4, 1..4] of Integer;
begin
for iCol := 4 downto 1 do
begin
for iRow := 4 downto 1 do
begin
if (gdSelected in State) then
begin
case arrState[ARow, aCol] of
0: begin
StringGrid.Canvas.Brush.Color := clWhite;
Rect := StringGrid.CellRect(iCol, iRow);
StringGrid.Canvas.FillRect(Rect);
Inc(arrState[iRow, iCol]);
end;
1: begin
StringGrid.Canvas.Brush.Color := clRed;
Rect := StringGrid.CellRect(iCol, iRow);
StringGrid.Canvas.FillRect(Rect);
Inc(arrState[iRow, iCol]);
end;
2: begin
StringGrid.Canvas.Brush.Color := $008CFF;
Rect := StringGrid.CellRect(iCol, iRow);
StringGrid.Canvas.FillRect(Rect);
Inc(arrState[iRow, iCol]);
end;
3: begin
StringGrid.Canvas.Brush.Color := clGreen;
Rect := StringGrid.CellRect(iCol, iRow);
StringGrid.Canvas.FillRect(Rect);
arrState[iRow, iCol] := 0;
end;
end;
end;
end;
end;
end;
【问题讨论】: