TNumBox是单元NumBox里自定义的类。
注意Text定义在delphi自带的Controls单元:
property Text: TCaption read GetText write SetText;
Unit1单元代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses NumBox;
var NumBox1: TNumBox;
procedure TForm1.FormCreate(Sender: TObject);
begin
NumBox1 := TNumBox.Create;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
NumBox1.AddOne;
Text := IntToStr(NumBox1.GetCount);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
NumBox1.AddFive;
Text := IntToStr(NumBox1.GetCount);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
NumBox1.ZeroCount;
Text := IntToStr(NumBox1.GetCount);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
NumBox1.Free;
end;
end.
form1窗体代码:
object Form1: TForm1
Left = 450
Top = 340
Width = 192
Height = 237
Caption = ‘Form1‘
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = ‘MS Sans Serif‘
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
OnDestroy = FormDestroy
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 45
Top = 48
Width = 75
Height = 25
Caption = ‘加一‘
TabOrder = 0
OnClick = Button1Click
end
object Button2: TButton
Left = 48
Top = 96
Width = 75
Height = 25
Caption = ‘加五‘
TabOrder = 1
OnClick = Button2Click
end
object Button3: TButton
Left = 48
Top = 144
Width = 75
Height = 25
Caption = ‘清空‘
TabOrder = 2
OnClick = Button3Click
end
end
NumBox单元代码:
unit NumBox;
interface
type
TNumBox = class
private
FCount: Integer;
public
procedure AddOne;
procedure AddFive;
procedure ZeroCount;
function GetCount: Integer;
end;
implementation
{ TNumBox }
procedure TNumBox.AddOne;
begin
Inc(FCount);
end;
procedure TNumBox.AddFive;
begin
Inc(FCount,5);
end;
procedure TNumBox.ZeroCount;
begin
FCount := 0;
end;
function TNumBox.GetCount: Integer;
begin
Result := FCount;
end;
end.