在对象的事件区中,Sender是用来指出哪个组件接收此事件,而调用事件的Handler
(事件句柄),而且能通过Sender参数来,让多个组件共享一个事件区.
在该事件区中,用Sender参数作为条件语句的分支条件,就能依据不同组件接收的
情况,而有不同的表现,
(事件句柄),而且能通过Sender参数来,让多个组件共享一个事件区.
在该事件区中,用Sender参数作为条件语句的分支条件,就能依据不同组件接收的
情况,而有不同的表现,
1unit Unit1;
2
3interface
4
5uses
6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, StdCtrls;
8
9type
10 TForm1 = class(TForm)
11 Label1: TLabel;
12 Button1: TButton;
13 Button2: TButton;
14 Button3: TButton;
15 Button4: TButton;
16 procedure Button1Click(Sender: TObject);
17 private
18 { Private declarations }
19 public
20 { Public declarations }
21 end;
22
23var
24 Form1: TForm1;
25
26implementation
27
28{$R *.dfm}
29
30procedure TForm1.Button1Click(Sender: TObject);
31VAR
32 SenderNum: Integer;
33begin
34 SenderNum := 1;
35 IF Sender = Button1 THEN SenderNum := 1;
36 IF Sender = Button2 THEN SenderNum := 2;
37 IF Sender = Button3 THEN SenderNum := 3;
38 IF Sender = Button4 THEN SenderNum := 4;
39 CASE SenderNum OF //
40 1: Label1.Top := Label1.Top - 10 ;
41 2: Label1.Top := Label1.Top + 10 ;
42 3: Label1.Left := Label1.Left - 10 ;
43 4: Label1.Left := Label1.Left + 10 ;
44 END; // case
45
46end;
47
48end.
49
50
2
3interface
4
5uses
6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, StdCtrls;
8
9type
10 TForm1 = class(TForm)
11 Label1: TLabel;
12 Button1: TButton;
13 Button2: TButton;
14 Button3: TButton;
15 Button4: TButton;
16 procedure Button1Click(Sender: TObject);
17 private
18 { Private declarations }
19 public
20 { Public declarations }
21 end;
22
23var
24 Form1: TForm1;
25
26implementation
27
28{$R *.dfm}
29
30procedure TForm1.Button1Click(Sender: TObject);
31VAR
32 SenderNum: Integer;
33begin
34 SenderNum := 1;
35 IF Sender = Button1 THEN SenderNum := 1;
36 IF Sender = Button2 THEN SenderNum := 2;
37 IF Sender = Button3 THEN SenderNum := 3;
38 IF Sender = Button4 THEN SenderNum := 4;
39 CASE SenderNum OF //
40 1: Label1.Top := Label1.Top - 10 ;
41 2: Label1.Top := Label1.Top + 10 ;
42 3: Label1.Left := Label1.Left - 10 ;
43 4: Label1.Left := Label1.Left + 10 ;
44 END; // case
45
46end;
47
48end.
49
50