Return to Sender(2)
2008-04-09 04:28:07来源:互联网 阅读 ()
procedure TForm1.Panel1Click(Sender: TObject);
begin
Panel1.Color := TPanel(Sender).Color;
end;
Since Sender provides a "return address" to the calling object, that object''''s properties can be read and set as well. This way, the procedure can also change the panel''''s Color property without actually working with the panel object''''s name. For example:
TPanel(Sender).Color := clRed;
Any type of property can be accessed using this framework, provided it''''s a property belonging to the specified type. In the previous example, the procedure specifies that Sender is a TPanel, which therefore has a Color property. While Sender is declared as a TObject in the procedure header, the following line would return a compiler error.
TPanel1.Color := TObject(Sender).Color;
As TPanel''''s ancestor type, TObject is perfectly valid in other respects; but it does not contain a Color property. Since TObject contains no properties, developers cannot use it to determine the properties of a wide range of objects. This means a program cannot find the color of a TPanel, TLabel, or TForm all with the same TObject(Sender).Color code.
In fact, these three component types all contain the Color property. However, because they do not share this property in common ancestor classes, programmers cannot easily create one block of code to get the Color property from these various types of objects. The simplest solution a developer could use would be this:
if Sender is TPanel then
Brush.Color := TPanel(Sender).Color;
if Sender is TLabel then
Brush.Color := TLabel(Sender).Color;
if Sender is TForm then
Brush.Color := TForm(Sender).Color;
Fortunately for developers, the Delphi designers added the Tag property. Tag is a little catch-all integer parameter that developers can use for whatever purpose they choose. What makes Tag especially useful is that it resides well up the inheritance tree in the TComponent class. Just two steps down the ladder from TObject, TComponent is the ancestor of all controls. This means that any of these objects will have the Tag property. Unlike the Color property cited above, the Tag property of any object can be accessed with a simple line of code:
TForm.Tag := TComponent(Sender).Tag;
Sender Impostors
I mentioned earlier that as procedures pass the Sender parameter from one to another, it never changes. It always points back to the object that called the original event handler. This is true provided each procedure along the path passes the original parameter. However, you can also substitute another parameter for the original Sender, fooling subsequent procedures into acting on another object as if it were the true Sender.
This is the basis of our sample application, SENDER.DPR. It uses Sender and the Tag property so that Delphi can be "fooled" into having similar menu and button commands perform special actions on the buttons. Traditionally, this wouldn''''t require special Sender parameters - both buttons and menu items will share an event handler that can update the button as part of its functionality:
procedure Button1Click(Sender: TObject);
begin
{ Main functionality }
Button1.Font.Style := [fsBold];
end;
However, when several components share a single event handler, the programmer cannot simply assume to act on a specific object. Modifying the code in question to read:
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
