Search code examples
delphipascallazarus

Delphi Spinedit: Get the previous value


Lets say I have a Spinedit on my Form.

The current Value of the Spinedit is e.g 5

When the user clicks on the SpinButton the next Value could be

4 or 6.

In the onChange event

I can get the new Value

4 or 6

but I need to know also the old Value

5

How can I get the previous value 5 in Delphi?

I need to know the old and the new Value


Solution

  • Store the previous value in a variable at the end of the OnChange event and at startup as below.

    unit Unit1;
    
    interface
    
    uses
      Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
      Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Samples.Spin;
    
    type
      TForm1 = class(TForm)
        SpinEdit1: TSpinEdit;
        procedure SpinEdit1Change(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
        FSpinPrev : Integer;
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      FSpinPrev := SpinEdit1.Value;
    end;
    
    procedure TForm1.SpinEdit1Change(Sender: TObject);
    begin
      if SpinEdit1.Value > FSpinPrev then Caption := 'Increasing'
                                     else Caption := 'Decreasing';
    
      FSpinPrev := SpinEdit1.Value;
    end;
    
    end.