在Procedure Tform1.Button2click(Sender:Tobjet)中
Sender是什么?
起什么作用?
如果对Button2进行复制,得到Button3好像可以进Button2的Button2click事件
为什么?
急需解答?
请给完成代码,最好由注释?
明白给分,谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!
Sender是什么?
起什么作用?
如果对Button2进行复制,得到Button3好像可以进Button2的Button2click事件
为什么?
急需解答?
请给完成代码,最好由注释?
明白给分,谢谢!谢谢!谢谢!谢谢!谢谢!谢谢!
解决方案 »
- 请教大侠关于delphi 控件问题 急!!!!!!!!!!!!!!!!!!
- ADOQUERY缓存更新如何设置?
- 这个STRING会结果显示什么 ?
- 网络检测方法 向大家请教
- 新手问题:关于shellexecute调用外部程序后,关闭的问题.
- 如何用DELPHI连接远程SQL SERVER 7
- 出現 作業系統無法支援選取的排序法則 錯誤,誰見到過?
- win2000与win98势不两力,怎么办啊?
- 如何学习Windows Api (100分大卖场)!
- 请问!!如何吧execl的数据导入到sqlserver里面??
- 请教两个的问题;问题解决不了连年也过不上了 5555
- 类型转换问题,请大家帮忙!!!!
如果对Button2进行复制,得到Button3好像可以进Button2的Button2click事件是因为它们同时指向同一个事件,如果你不想这样的话可以将新按钮的click事件删除,再重新定义它的事件!
2。復制是有時會出現這樣的情況。
if Sender is TButton(其他类都可以) then
//判断以前的控件是不是按钮
====================
原始作者:林毅 一、Owner和Parent的区别: Parent属性是指构件的包容器,构件只能在此范围内显示和移动。举例子如下:
(1)在Form1的窗体上,放一个Panel1,并将Panel1拉大,
(2)在Panel1上放一Button1;
(3)在Form1上放一Button2。
现在如果移动Panel1,则Button1随着Panel1移动,这是因为Button1的Parent是Panel1。
现在将Button2移到Panel1上,再次移动Panel1,Button2并不跟着移动,这是因为Button2的
Parent是Form1。除在窗体设计中,应注意构件的Parent是谁外,在动态创建构件时,也应
指出构件的Parent,如在上例中继续操作:
1)Procedure Tform1.Button2click(Sender:Tobjet);
2)Var
3) Button:Tbutton;
4) Begin
5) Button:Tbutton.cerate(self);
6) Button.parent=panel1;
7) Button.lleft=0;
8) Button.top=0;
9) Button.caption:='OK';
10) End;
当按Button2时,将在Panel1上创建一个Button,而如果把第6句改为Button.parent:=self;
按Button2时,将在Form1上创建一个Button了。如果将第6句删除,按Button2时,什么都不会发
生,这是因为创建方法无法知道应在哪里显示构件。
Owner属性是指构件的所有者,它负责构件的创建和释放。如在上例中,系统默认窗体上所有
构件的所有者是窗体,而窗体的所有者是Application。顺便指出,create方法应带有表示构件所
有者的参数,如在上例中,构件所有者是窗体,即self。
Parent属性和Owner属性是运行阶段的属性,只能在运行阶段,通过代码设置。
二、Self和Sender的区别: 在事件处理程序参数表中,至少含有一个参数Sender,它代表触发事件处理程序的构件,如在
上例中,Sender就指Button2,有了Sender参数,可以使多个构件共用相同的事件处理程序,如下例:
Procedure Tform1.Buttonclick(Sender:Tobject);
Begin
If sender=button1 then
Label1.caption:=′看庭前花开花落
′ Else Label2.caption:=′望天上云卷云舒′
End;
在此例中,Button1,Button2共用了Buttonclick事件处理程序。
Self是指所编的程序范围是在哪一个类中,Delphi中大都在窗体范围内编程,因此,self即指
窗体,如果在编写一个类或是一个组件,则self指该类或组件。我们在函数或过程的声明中可看出
self是代表哪个组件,即self代表‘.’号之前的组件,如在第一个例子中,self代表Tform1。另外
应注意,self只能用在类方法中,而不能用在过程或函数中,如下例用法是错的:
Function a1(B:Integer):Integer;
Begin
……
Button:=tbutton.create(self);……
End;
三、Clientheight和Height,Clientwidth和Width的区别: 对于一般的构件而言,Height就是Clientheight,Width就是Clientwidth,而对于窗体而言,
Height是包括标题条在内的高度,而Clientheight是指窗体工作区的高度。同理,Clientwidth是
指定窗体工作区的宽度。
从上面陈述可知,理解Ower和Parent,Self和Sender,Clientheight和Height,Clientwidth和
Width区别,对于Delphi中正确编程是重要的。
如:
Procedure Tform1.Button2click(Sender:Tobjet);
Sender 就是 Button2
如果你已经给一个按钮事件处理程序,再复制它,那边复制出来的按钮用的是原按钮的事件处理程序。要更换,请在 Object Inspector 中切换到 Events ,单击后面的事件处理程序,然后删除再双击添加即可。
Self--意义:指本类,也就是Self被引用的类。比如若在类TMyClass内引用了Self,那么Self=TMyClass.
Owner--意义:哪个对象释放我的内存啊?如:Pan:=TPanel.Create(Self);其中Create的参数是:AOwner:TComponent。Owner释放Pan的内存。因为窗口释放Pan的内存,但窗口类的对象是Self.一般给Owner传Self就可以。
比如:
代码段一:
pan:=TPanel.Create(Self);
with Pan do begin
try
Left:=20;
Top:=20;
parent:=Self; //Parent:=Form1也可以。
Visible:=true;
ShowMessage('Created');
finally
Pan.free;
end;
end;
------
private
{ Private declarations }
procedure AssignOnContextPopupEvent;
procedure OnContextPopup(Sender: TObject; MousePos: TPoint;
var Handled: Boolean);
public
{ Public declarations }
end;var
Form1 : TForm1;implementation{$R *.dfm}
uses
TypInfo;procedure TForm1.MonthCalendar1GetMonthInfo(Sender: TObject;
Month: Cardinal; var MonthBoldInfo: Cardinal);
begin
if Month = 9 then { April}
MonthCalendar1.BoldDays([3, 21, 28], MonthBoldInfo); { Day 3, 21, 28 }
end;procedure TForm1.OnContextPopup(Sender: TObject; MousePos: TPoint;
var Handled: Boolean);
begin
with Sender as TComponent do
ShowMessage(Name + ' right-clicked!');
end;procedure TForm1.AssignOnContextPopupEvent;
var
i : Integer;
PropInfo : PPropInfo;
Method : TMethod;
PEvent : ^TContextPopupEvent;
begin
for i := 0 to ComponentCount - 1 do
begin
PropInfo := GetPropInfo(Components[i].ClassInfo, 'OnContextPopup');
if (PropInfo <> nil) and (PropInfo^.PropType^^.Kind = tkMethod) then
begin
Method := GetMethodProp(Components[i], PropInfo);
if not Assigned(Method.Code) then
begin
PEvent := @Method.Code;
PEvent^ := OnContextPopup;
Method.Data := Self;
SetMethodProp(Components[i], PropInfo, Method);
end;
end;
end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
AssignOnContextPopupEvent;
end;
*****************************************
如果要清除所有的事件也可以!
//使用 GetPropCount 可取得控件的的属性计数:function GetPropCount(Instance: TPersistent): Integer;
var
Data: PTypeData;
begin
Data := GetTypeData(Instance.Classinfo);
Result := Data^.PropCount;
end;//使用 GetPropName 可取得属性的名称:function GetPropName(Instance: TPersistent; Index: Integer): string;
var
PropList: PPropList;
PropInfo: PPropInfo;
Data: PTypeData;
begin
Result := '';
Data := GetTypeData(Instance.Classinfo);
GetMem(PropList, Data^.PropCount * Sizeof(PPropInfo));
try
GetPropInfos(Instance.ClassInfo, PropList);
PropInfo := PropList^[Index];
Result := PropInfo^.Name;
finally
FreeMem(PropList, Data^.PropCount * Sizeof(PPropInfo));
end;
end;procedure CloseForm(Form: TForm);
var
Index: Integer;
src: TPersistent;
SrcPropInfo: PPropInfo;
MyMethod: TMethod;
j: integer;
begin
with Form do
begin
for j := 0 to ComponentCount - 1 do
begin
src := TPersistent(Components[j]);
for Index := 0 to GetPropCount(Src) - 1 do
begin
SrcPropInfo := GetPropInfo(Src.ClassInfo, GetPropName(Src, Index));
if (srcPropInfo <> nil) and (SrcPropInfo^.PropType^.Kind = tkMethod)
then
begin
MyMethod := GetMethodProp(Src, SrcPropInfo);
if (Assigned(MyMethod.Data)) and (Assigned(MyMethod.Code)) then
begin
MyMethod.Data := nil;
MyMethod.Code := nil;
SetMethodProp(Src, SrcPropInfo, MyMethod);
end;
end;
end;
end;
end;
end;