ReRe: Delphi Timer.Interval / cardinal value

By John, DelphiLand Team

In Reply to: Re: Delphi Timer.Interval / cardinal value posted by Stefan Loeners

: I had tried your suggestion in the first place. However, it does not work.

Below is the source code for a simple Delphi project's unit, that can give you clues as to where things go wrong.

unit Unit1;

interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, StdCtrls, IniFiles;
type
  TForm1 = class(TForm)
    Timer1: TTimer;
    Label1: TLabel;
    Button1: TButton;
    procedure Timer1Timer(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    OldInterval: cardinal;
  public
    { Public declarations }
end;
var
  Form1: TForm1;
 
implementation

{$R *.DFM}
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  Label1.Caption := IntToStr(Timer1.Interval);
  OldInterval := Timer1.Interval;
end;
 
{ Change the value of Timer.Interval,
  either to a value read from an ini-file,
  or back to the old value that it had when the program started }
procedure TForm1.Button1Click(Sender: TObject);
var
  Ini: TIniFile;
begin
  if Timer1.Interval = OldInterval then begin
    Ini := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));
 
    { We use OldInterval for the default value, meaning: the value
      to return if the Section doesn't exist, or if the Key doesn't
      exist, or if the data value for Key is not assigned. }
    Timer1.Interval := Ini.ReadInteger('Timer1', 'Interval', OldInterval);
    Ini.Free;
  end
  else
    Timer1.Interval := OldInterval;
  Label1.Caption := IntToStr(Timer1.Interval);
end;
 
{ Change the Form's color on every OnTimer event }
procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if Color = clBtnFace then
    Color := clLime
  else
    Color := clBtnFace;
end;
 
end.

This code has been tested right before I posted it here, so it *must* work if copied exactly ;)
If you name your project as "TimerTest", then create a file TimerTest.ini in the same directory, with the following contents:

[Timer1]
Interval=500