打印本文 打印本文 关闭窗口 关闭窗口
Return to Sender
作者:武汉SEO闵涛  文章来源:敏韬网  点击数2529  更新时间:2009/4/23 18:39:24  文章录入:mintao  责任编辑:mintao
s 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 :=

    TCompone

上一页  [1] [2] [3]  下一页

打印本文 打印本文 关闭窗口 关闭窗口