2009. október 7., szerda

Make the cell of a TStringGrid flash when its value changes


Problem/Question/Abstract:

I want the cell in a TStringGrid to flash for a few seconds when its value changes (as a result of some outside monitored process for example). How would I do that?

Answer:

One way would be to check the contents of the cell against the previous value in the OnDrawCell event. When it has changed, start a timer which invalidates the grid on a set interval. Below you'll find an example for just one cell.

{Somewhere in the private section of the form}
var
  FToggleCount: integer;
  FCheckstring: string;

implementation

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Inc(FToggleCount);
  if FToggleCount >= 10 then
    Timer1.Enabled := False;
  StringGrid1.Invalidate;
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  s: string;
begin
  if (ACol = 1) and (ARow = 1) then
    with Sender as TStringGrid do
    begin
      s := Cells[ACol, ARow];
      if s <> FCheckstring then
      begin
        FCheckstring := s;
        FToggleCount := 0;
        Timer1.Enabled := True;
      end;
      if Timer1.Enabled and ((FToggleCount mod 2) = 0) then
      begin
        Canvas.Brush.Color := clRed;
        Canvas.FillRect(Rect);
        Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, s);
      end;
    end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése