2.Sender 结合AS操作符进行类型转换 将若干个派生于某一父类的子类强制转换成该父类。 例如表单中有一个TEdit类控件和一个TMemo控件,它们实际上都派生于TcustomEdit类,如果你要为二者的某一事件提供同样处理,可以将二者事件句柄都指向自定义的过程yyy: Procedure TForm1.yyy(Sender:TObject); begin (sender as TcustomEdit).text:=′This is some demo text′; end; 在过程中,AS操作符将TEdit类和TMemo类均强制转换成TcustomEdit类,再对TcustomEdit类的属性赋值(资料引自网上搜索)
1.TStatusBar.panels[].width可以设置宽度啊;2.Sender as是强制类型转换啊;举个例子看其用途之一:比如Button1的事件: procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage(Button1.Caption); end;和Button2的事件: procedure TForm1.Button2Click(Sender: TObject); begin ShowMessage(Button2.Caption); end;可以简化成: Button1.Click=MyButtonClick; Button2.Click=MyButtonClick;而MyButtonClick定义为: procedure TForm1.MyButtonClick(Sender: TObject); begin ShowMessage((Sender as TButton).Caption); //或者是ShowMessage(TButton(Sender).Caption); end;
1.TStatusBar.panels[].width可以设置宽度 2.Sender as TToolButton as 功能是将左边的对象转换为右边类类型的实例,左边的对象必须满足以下条件: 1)为右边类类型的实例 2)为右边类类型后代的实例 3)nil 否则将导致异常. 运算的结果为右边类类型的实例
statusbar1.panels[0].width:=200;
-----panels[0]是第一栏,panels[1]是第二栏,依次.....
将若干个派生于某一父类的子类强制转换成该父类。
例如表单中有一个TEdit类控件和一个TMemo控件,它们实际上都派生于TcustomEdit类,如果你要为二者的某一事件提供同样处理,可以将二者事件句柄都指向自定义的过程yyy: Procedure TForm1.yyy(Sender:TObject);
begin
(sender as TcustomEdit).text:=′This is some demo text′;
end; 在过程中,AS操作符将TEdit类和TMemo类均强制转换成TcustomEdit类,再对TcustomEdit类的属性赋值(资料引自网上搜索)
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(Button1.Caption);
end;和Button2的事件:
procedure TForm1.Button2Click(Sender: TObject);
begin
ShowMessage(Button2.Caption);
end;可以简化成:
Button1.Click=MyButtonClick;
Button2.Click=MyButtonClick;而MyButtonClick定义为:
procedure TForm1.MyButtonClick(Sender: TObject);
begin
ShowMessage((Sender as TButton).Caption);
//或者是ShowMessage(TButton(Sender).Caption);
end;
2.Sender as TToolButton
as 功能是将左边的对象转换为右边类类型的实例,左边的对象必须满足以下条件:
1)为右边类类型的实例
2)为右边类类型后代的实例
3)nil
否则将导致异常.
运算的结果为右边类类型的实例