分享
 
 
 

Return to Sender

王朝delphi·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

Return to Sender

The Lowly Sender Parameter Can Make Applications Shine

The humble Sender parameter could be one of Delphi's most useful tools for modular, extensible programming. Although Sender is normally used to simply determine which object called a particular procedure, with some creativity, this parameter can be extended to allow robust modular programming with simple, reusable code.

Sender Basics

The Sender parameter is so ingrained in Delphi programming that it appears in practically every program procedure. Even the Form's OnCreate event - which has no sender - includes the (Sender: TObject) parameter in its definition.

Despite its ubiquity, the Sender parameter is not necessary for a procedure to function. Removing Sender from a procedure that doesn't use it results in perfectly functional code, as long as the parameter is also removed from the procedure's type statement in the form object's interface section. Delphi will generate an "incompatible parameter" warning at compile time, but this can be safely ignored. In fact, removing any unused Sender parameters results in slightly leaner and meaner code.

Not only can Sender be omitted from any procedure, but when used, it doesn't even have to be called Sender. This name is only used by convention. The common term makes understanding code simpler, but there's no reason not to name it Caller, Origin, or Banana if the developer prefers.

So the Sender parameter doesn't have to be called Sender, and is, in fact, unnecessary. So what is it? And why is it important enough to add to practically every Delphi procedure?

Sender is simply a parameter passed from the component that called the event handler. Sender (or whatever it's called by the event handler) is a parameter of the type TObject. Because TObject is the ancestor of all components, Sender can accommodate any Delphi object that can possibly trigger an event. Sender forms a two-way link between an object and an event handler; it tells the procedure what object triggered it to be called. In other words, if an event handler was a letter, the Sender parameter would be the return address.

Using Sender

The simplest and most common way to use Sender is with an if...then or case statement that performs a certain action depending on what object triggered the procedure. Here's a typical example:

procedure Form1.ButtonClick(Sender: TObject);

begin

if Sender is StopButton

then Stop(Sender);

end;

This code examines the Sender parameter to see if it's the object, StopButton. If so, the event handler calls another procedure, Stop, passing the Sender parameter to that procedure as well. The Stop procedure can call another procedure and so on, passing the Sender parameter through the program code. Because each procedure is simply passing along the parameter it received, wherever the parameter is ultimately used, it will reflect the object that called the original event handler.

Not only can Delphi developers use Sender to identify the object that called a procedure, but they can also use it to access that object's properties. The form in Figure 1 uses a number of colored panels as a palette. When the user clicks on a panel, the large panel on the left changes to the color of the selected panel.

Figure 1: This mini-program sets the color of the panel at right to the color of the panel selected by the user.

This procedure does not need the name of the panel selected, only its color property. Therefore, each panel in the palette can have its own color, but all share the following OnClick event handler:

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:

TButton(Sender).Font.Style := [fsBold];

is fine provided the code is always called by a TButton. However, this makes it troublesome to call the same event handler from a corresponding menu item because the menu item will cause an error each time it calls the event handler.

The Sample Application's Framework

The sample application provides the framework for a modular, easily-extendible application. It also demonstrates how to make the simple Sender parameter do a lot of high-powered work, and shows how to get around some of the limitations mentioned earlier.

Most applications contain menu items and buttons that perform the same action. Delphi makes it simple to do this by routing multiple events to a shared event handler. If multiple buttons and menu items all use the same event handler, it's simple to determine which control called the procedure. However, it becomes more difficult to determine, say, which menu item corresponds to the calling event if the control was actually a button. The user may obviously find that the Word Wrap button and the Word Wrap menu command are synonymous in functionality. However, to have Delphi understand this and update the menu item's Checked property - regardless of which control was the Sender - takes some special programming.

The sample application contains four SpeedButtons, as well as main and pop-up menu items that correspond to each (see Figure 2). The buttons and menu items each perform similar functions, in this case, using a database to track the amount of time spent on each of a number of tasks. Program users can configure any number of tasks, all of which call the same event handler. (For the sake of clarity, the sample application illustrates only the Sender functions.)

Figure 2: The layout of the sample application's components.

While a task is active, its SpeedButton remains depressed. Pressing a depressed button releases it and ends the task. Pressing a button while another is depressed releases the first and depresses the new button, causing the timesheet database to be simultaneously updated. The trick, of course, is to handle a number of MenuItems and SpeedButtons all with one event handler. This technique enables the programmer to allow for any number of tasks without having to write a new handler for each.

Implementing this functionality requires one more ingredient: the Components property. Like Tag, Components is a property of TComponent, meaning that it's accessible to all components. Components is a property of every component, but in most cases it's only used on TForms. In this case, the Components property is an array of all components owned by the form.

Build It

To build the sample application, create a single form named Form1. Add four SpeedButtons (used here to easily allow two-state buttons), a Label, a MainMenu (with an item called File1 and four sub-items), and a pop-up menu (also with four menu items). Set the properties as shown in Figure 3.

SpeedButtons

Name/Caption

AllowAllUp

GroupIndex

Tag

OnClick event handler

SpeedButton1

True

1

1

SpeedButtonClick

SpeedButton2

True

1

2

SpeedButtonClick

SpeedButton3

True

1

3

SpeedButtonClick

SpeedButton4

True

1

4

SpeedButtonClick

MenuItems

Name/Caption

Tag

OnClick event handler

File1

0

--

MainItem1

1

SpeedButtonClick

MainItem2

2

SpeedButtonClick

MainItem3

3

SpeedButtonClick

MainItem4

4

SpeedButtonClick

PopupItem1

1

SpeedButtonClick

PopupItem2

2

SpeedButtonClick

PopupItem3

3

SpeedButtonClick

PopupItem4

4

SpeedButtonClick

Figure 3: Setting the values of properties for the sample application's SpeedButton and MenuItem components.

Note that all the buttons and menu items have the same event handler, namely SpeedButtonClick (see Figure 4). This procedure is called from any of the SpeedButtons or menu items on the form (except MenuItem File1). It references two external procedures, PushButton and ReleaseButton.

procedure TForm1.SpeedButtonClick(Sender: TObject);

var

A : Integer;

begin

if Sender is TSpeedButton then

if TSpeedButton(Sender).Down then

PushButton(Sender)

else

ReleaseButton(Sender)

else

for A := 0 to ComponentCount-1 do

if Components[A] is TSpeedButton then

with Components[A] as TSpeedButton do

if Tag = TComponent(Sender).Tag then

begin

Down := not Down;

if Down then

PushButton(Self.Components[A])

else

ReleaseButton(Self.Components[A]);

end;

end;

Figure 4: Referencing PushButton and ReleaseButton with the SpeedButtonClick procedure.

SpeedButtonClick first determines which object sent the event. If it's a TSpeedButton, it simply passes that object to the PushButton or ReleaseButton procedure, depending on the button's state (up or down). These procedures simply allow convenient spots for the messages to be handled. In the following example, the Caption of Label1 is set to report the event that occurred:

procedure TForm1.PushButton(Sender: TObject);

begin

Label1.Caption :=

TComponent(Sender).Name + ' was clicked.';

end;

procedure TForm1.ReleaseButton(Sender: TObject);

begin

Label1.Caption :=

TComponent(Sender).Name + ' was released.';

end;

If the Sender of the event is not one of the buttons, we must determine which button to click. This is where the Tag property and the Components array property come in. As you noticed in Figure 3, SpeedButton1 had a Tag value of 1. MainItem1 and PopupItem1 also have Tag values of 1. Therefore, if any of the menu items are selected, it's a simple matter of viewing their Tag values and transferring the event to the SpeedButton with the same Tag. This is handled in the SpeedButtonClick handler:

for A := 0 to ComponentCount-1 do

if Components[A] is TSpeedButton then

with Components[A] as TSpeedButton do

if Tag = TComponent(Sender).Tag then

This code snippet scans the Components array and examines each component it finds. When a TSpeedButton is found, the code compares this object's Tag value with the Tag value of the object that originally fired the message. If the values are the same, then the code found the button that ultimately receives the event. Since the code has already determined that it's a TSpeedButton, this object's up/down state can now be toggled. Then, either the PushButton or ReleaseButton procedure is called with the selected SpeedButton passed as the parameter as follows:

begin

Down := not Down;

if Down then

PushButton(Self.Components[A])

else

ReleaseButton(Self.Components[A]);

end;

This statement:

PushButton(Self.Components[A])

calls the procedure PushButton, passing along the parameter:

Self.Components[A]

The PushButton procedure interprets this Self statement as the original Sender. The Self portion is required because we want to look at the Components property of Form1. Since the code uses a with statement to simplify readability, we must ensure that the code is viewing the correct Components property. In this case, SpeedButtons also have a Components property and without the Self qualifier, it will erroneously look there instead.

The PushButton procedure is where the timesheet database would be updated or other functions would be performed. Since the previous procedure handled the bulk of the work, PushButton simply performs the actions necessary when the button has been pressed. Remember that SpeedButtonClick did not send the actual Sender parameter, but sent a modified one that points to the SpeedButton regardless of how the button was selected. SpeedButtonClick's other procedure call, ReleaseButton, is likewise called to handle whatever events should occur when the button is released.

Conclusion

Alone, the Sender parameter is a simple return address, allowing functions to trace which component triggered their event. But used in conjunction with a few other properties, such as Tag and Components, Sender becomes a powerful tool for tracing and filtering program flow. This ability to work around program limitations allows developers to create single, modular event handlers for components of all types, without the need for multiple if..then statements. This, of course, is just one way of extending Sender's abilities. There are as many possibilities as there are programs to write.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有