Return to Sender(3)

2008-04-09 04:28:07来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

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

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Data-Browse型Data-Aware控件的制作

下一篇:ktv 系统设计经验