title: Delphi 7: How can I change the colors of individual cells on a StringGrid via clicking on them?
body: <p>I am trying to make an app where the cells of a <code>TStringGrid</code> will change color when I click on them. Each time I click on a cell, it should switch over to the next color and stay that color, until I click on that cell again, in order:</p>
  
  <p>white ==> red ==> orange ==> green ==> white (like a traffic light).</p>
  
  <p>The error(s) I am getting is a bit hard to explain, but I'll try.</p>
  
  <p>The application runs, but when I click on one cell and then on another, sometimes the first cell I clicked on changes color, but the second one doesn't.  Other times, both cells change color.  Other times, both cells just reset to their white state.</p>
  
  <pre><code>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;
  </code></pre>