Author: Tomas Rutkauskas
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.
1 {Somewhere in the private section of the form}2 var3 FToggleCount: integer;
4 FCheckstring: string;
5 6 implementation7 8 procedure TForm1.Timer1Timer(Sender: TObject);
9 begin10 Inc(FToggleCount);
11 if FToggleCount >= 10 then12 Timer1.Enabled := False;
13 StringGrid1.Invalidate;
14 end;
15 16 procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
17 Rect: TRect; State: TGridDrawState);
18 var19 s: string;
20 begin21 if (ACol = 1) and (ARow = 1) then22 with Sender as TStringGrid do23 begin24 s := Cells[ACol, ARow];
25 if s <> FCheckstring then26 begin27 FCheckstring := s;
28 FToggleCount := 0;
29 Timer1.Enabled := True;
30 end;
31 if Timer1.Enabled and ((FToggleCount mod 2) = 0) then32 begin33 Canvas.Brush.Color := clRed;
34 Canvas.FillRect(Rect);
35 Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, s);
36 end;
37 end;
38 end;