转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> Delphi程序 >> 正文
Return to Sender         ★★★★

Return to Sender

作者:闵涛 文章来源:闵涛的学习笔记 点击数:2528 更新时间:2009/4/23 18:39:24
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]  下一页


[办公软件]在Excel中插入时间/日期TODAY和NOW函数  [网络安全]激活型触发式AutoRun.inf病毒和rose病毒的清除方案
[Web开发]asp.net c#其它语句之break、continue、goto实例介…  [Web开发]一大堆常用的超强的正则表达式及RegularExpressio…
[平面设计]制作动态GIF图像的好帮手─Coffee GIf Animator  [平面设计]功能超强的GIF动画制作软件─Ulead Gif Animator
[平面设计]AutoCAD常用快捷命令(外加中文说明)  [平面设计]AutoCAD常用快捷命令(字母类,外加中文说明)
[平面设计]AutoCAD快捷命令介绍  [平面设计]多种方法解决AutoCAD打印时出现错误
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台