【问题标题】:Delphi Spinedit: Get the previous valueDelphi Spinedit:获取上一个值
【发布时间】:2021-03-22 11:39:29
【问题描述】:

假设我的表单上有一个 Spinedit。

Spinedit 的当前值为例如 5

当用户点击 SpinButton 时,下一个值可能是

4 或 6。

在 onChange 事件中

我可以得到新的价值

4 或 6

但我还需要知道旧的价值

5

如何在 Delphi 中获得之前的值 5?

我需要知道旧值和新值

【问题讨论】:

  • 我没有看到任何直接执行此操作的方法,但您可以在 OnChange 事件结束时自己轻松存储先前的值。您还需要在启动时初始化变量。
  • 可以继承TSpinEdit控件,自己添加这个功能

标签: delphi pascal lazarus


【解决方案1】:

在 OnChange 事件结束时和启动时将之前的值存储在变量中,如下所示。

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.

【讨论】:

  • 如果不想声明单独的变量,可以使用所有组件都有的通用Tag: NativeInt作为存储
猜你喜欢
  • 1970-01-01
  • 2019-06-07
  • 1970-01-01
  • 2012-09-07
  • 1970-01-01
  • 1970-01-01
  • 2012-04-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多