事件过程应用实例

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  Sum: int64;                                     //记录结果
  strMsg: string;                                 //显示结果信息
begin
  if (Sender As Tbutton).Tag = 1 then             //调用按钮的TAG属性
  begin
    Sum := strtoint(edit1.Text) + strtoint(edit2.Text);
    strMsg := '两者的和是:' + inttostr(Sum);
    showmessage(strMsg);                          //调用标准过程显示加法
  end
  else
    if (Sender As Tbutton).Tag = 2 then           //单击“乘法”按钮
    begin
      Sum := strtoint(edit1.Text) * strtoint(edit2.Text);
      strMsg := '两者的积是:' + inttostr(Sum);
      showmessage(strMsg);                        //调用标准过程显示乘积
    end;

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Form1.Button1Click(Sender);                     //调用“加法”按钮事件过程
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  close;                                          //关闭窗口
end;

end.

 

posted @ 2011-12-06 15:23  endsnow  阅读(161)  评论(0)    收藏  举报