http://www.cnblogs.com/ywangzi/archive/2012/09/06/2673414.html

delphi自定义事件处理

 

为什么我们点击按钮,就会执行按钮的onclick事件?这个事件是怎么和我们自己的代码关联起来的。相信很多人都有这个疑问。那么我们就通过一个自定义事件来了解这里面得运行原理。以乘法运算来显示,如果乘数大于100,就提示用户,太大了。这个事件用自定义事件定义。 大家先看一下最终效果,

我添加了三个文本框,一个按钮,当第一或者第二个文本框的数>100时,点求积按钮,提示Too Big 对话框。

完成自定义事件需要5步来做:

1.定义TNotifyEvent类型私有变量

FTooBig:TNotifyEvent;

FTooBig是一个指针,它可以保持事件方法的调用地址。

2.公布一个属性

published
  property OnTooBig:TNotifyevent read FTooBig write FTooBig;

OnTooBig这个属性用来操作FTooBig这个私有变量,因为FTooBig是个函数指针,所以但读这个变量时,也就调用了函数。

3.定义事件处理函数


         procedure TooBigEvent(Sender: TObject);

         procedure TForm1.TooBigEvent(Sender: TObject);
         begin
         Application.MessageBox('Too Big','Test Event!',MB_OK);
         end;

这是当文本框数太大时执行的方法。

4. 把事件处理函数赋值给TNotifyEvent私有变量

FTooBig := TooBigEvent;

5. 当条件符合时执行事件处理函数


procedure TForm1.Button1Click(Sender: TObject);
var
  val1 : integer;
  val2 : integer;
  res  : integer;
begin
 val1 := StrToInt(Edit1.Text);
 val2 := StrToInt(Edit2.Text);
 if(val1<100)and(val2<100) then
 begin
 res := val1*val2;
 Edit3.Text := IntToStr(res);
 end
 else
 if assigned(FTooBig) then OnTooBig(Self);
end;

下面贴出全部代码:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls; type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Button1: TButton;
    procedure TooBigEvent(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private      FTooBig:TNotifyEvent; 
    { Private declarations }
  public
    { Public declarations }
  published
    property OnTooBig:TNotifyevent read FTooBig write FTooBig;
end; var
  Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject);
var
  val1 : integer;
  val2 : integer;
  res  : integer;
begin
 val1 := StrToInt(Edit1.Text);
 val2 := StrToInt(Edit2.Text);
 if(val1<100)and(val2<100) then
 begin
 res := val1*val2;
 Edit3.Text := IntToStr(res);
 end
 else
 if assigned(FTooBig) then OnTooBig(Self);
end;
procedure TForm1.TooBigEvent(Sender: TObject);
begin
Application.MessageBox('Too Big','Test Event!',MB_OK);
end; procedure TForm1.FormCreate(Sender: TObject);
begin
FTooBig := TooBigEvent;
end; end.

delphi自定义事件处理的更多相关文章

  1. 1.2异常处理和服务配置、aop、日志、自定义事件处理

    一.异常处理 2.1.数据验证 现在假设说要进行表单信息提交,肯定需要有一个表单,而后这个表单要将数据提交到 VO 类中,所以现在的基本实现如下: 1. 建立一个 Member.java 的 VO 类 ...

  2. Delphi自定义窗口过程WinProc

    unit ScWndProc; interface uses Forms, Messages; const DDGM_FOOMSG = WM_USER; //自定义消息 implementation ...

  3. commonJS — 自定义事件处理(for CustomEvent)

    for CustomEvent github: https://github.com/laixiangran/commonJS/blob/master/src/forCustomEvent.js 代码 ...

  4. Delphi 自定义窗体(最大化、最小化、关闭、窗体的移动)

    Uses ShellAPI; 1.//最小化procedure TForm1.btn1Click(Sender: TObject);var  I, J, X, Y: Word;begin  //第一种 ...

  5. delphi 自定义内存管理

    1.主要通过GetMemoryManager来hook原来的内存管理. 2.通过SetMemoryManager来设置你自己的新的内存管理,可以用一个内存池来优化和管理程序的内存调用情况. proce ...

  6. Delphi 7学习开发控件(续)

    继上次我们学习开发一个简单的画线控件后,基本的制作控件步骤已经清楚了,这次我们继续加深学习控件的制作.我们打开Delphi 7创建一个应用程序,拖动LineTo控件到窗体上,仔细看左边的对象设计器,可 ...

  7. Delphi属性比对象的域有更强的功能

    8.4 Delphi自定义组件(3) http://tech.163.com 2006-04-29 11:49:34 来源: 清华大学出版社 网友评论0 条 论坛   3. 测试未安装的组件 在将新组 ...

  8. 第38课 Qt中的事件处理(上)

    1. GUI程序原理回顾 (1)图形界面应用程序的消息处理模型 (2)思考:操作系统发送的消息如何转变为Qt信号 2. Qt中的事件处理 (1)Qt平台将系统产生的消息转换为Qt事件 ①Qt事件是一个 ...

  9. delphi 导出xml文件

    procedure TfrmTallageDetail.ToolButton1Click(Sender: TObject); var Xml: TXMLDocument; Rootbusiness,R ...

随机推荐

  1. npm学习(一)之安装、更新以及管理npm版本

    安装npm 安装前须知: npm是在Node中编写的,因此需要安装Node.js才能使用npm.可以通过Node.js网站安装npm,或者安装节点版本管理器NVM. 如果只是想开始探索npm,使用No ...

  2. 深度学习之depthwise separable convolution,计算量及参数量

    目录: 1.什么是depthwise separable convolution? 2.分析计算量.flops 3.参数量 4.与传统卷积比较 5.reference

  3. Houdini:也许是你未曾听过的最振奋人心的 CSS 进化

    原文链接:Houdini: Maybe The Most Exciting Development In CSS You’ve Never Heard Of更多译文将陆续推出,欢迎点赞+收藏+关注我的 ...

  4. MySQL中Innodb的聚簇索引和非聚簇索引

    聚簇索引 数据库表的索引从数据存储方式上可以分为聚簇索引和非聚簇索引(又叫二级索引)两种.Innodb的聚簇索引在同一个B-Tree中保存了索引列和具体的数据,在聚簇索引中,实际的数据保存在叶子页中, ...

  5. Apache 安装后启动出现的错误

    错误信息 1: configure: error: APR not found 解决方法:yum install apr* -y 错误信息 2:httpd: apr_sockaddr_info_get ...

  6. 2019 Multi-University Training Contest 4 1008K-th Closest Distance(二分+主席树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6621 题目大意:给一个数组,每次给 l ,r, p, k,问区间 [l, r] 的数与 p 的绝对值的 ...

  7. 费用流+SPFA ||Luogu P3381【模板】最小费用最大流

    题面:[模板]最小费用最大流 代码: #include<cstdio> #include<cstring> #include<iostream> #include& ...

  8. margin-top百分比问题

    (1)其实margin-top和margin-bottom的百分比,一般是按容器元素的宽度而不是高度来计算的,padding同理.

  9. LeetCode--094--二叉树的中序遍历(python)

    递归 # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # ...

  10. HTTP教程

    适合人群 本教程已为计算机学科毕业生和Web开发人员准备,帮助他们了解与超文本传输​​协议(HTTP)相关的基本到高级概念. 预备知识 在继续本教程之前,最好对Web概念,Web浏览器,Web服务器, ...