override deal with window closing in database application
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, Buttons, Grids, DBGrids;
type
TForm1 = class(TForm)
BtnModify: TBitBtn;
BtnCancel: TBitBtn;
BtnAdd: TBitBtn;
BtnDelete: TBitBtn;
BtnSave: TBitBtn;
BtnClose: TBitBtn;
DBGrid1: TDBGrid;
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure BtnSaveClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
protected
{Protected declarations}
function DataModifiedWithoutSaving():Boolean;virtual;abstract;
end;
var Form1:TForm1 ;
implementation
{$R *.dfm}
procedure TForm1.FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
begin
if DataModifiedWithoutSaving then
begin
if MessageDlg(‘’The data has be changed without saving, would you like save the data?‘’,mtWarning,[mbYes,mbNo],0) = mrNo then
begin
CanClose := True;
end else begin
BtnSaveClick(Self);
CanClose := True;
end;
end;
end;
procedure TForm1.BtnSaveClick(Sender: TObject);
begin
//
end;
end.
The above code is not very good (maybe you can give a more effect one), but it really can deal with the problem we mentioned at the beginning of the paper. But let us have a thought now; if we got twenty forms we should copy the code of the FormCloseQuery function twenty times. Do you think it is a boring thing? Maybe not, but how about that the team leader do not think the code you have written is very good and he gives you another one? Then copy again? I think it is needed to find an effect method to deal with this problem. The tool I show the example here is Delphi, of course many other tools are used in the software field such as Virsual C++, Virsual Basic and so on. But most of them (almost all of them) are the tools which support OOP (object-oriented
programming). The three main property of OOP are Encapsulation, Inheritance and Dynamic. We can use the Dynamic property to deal with the problem. To do so, we need a form which has the FormCloseQuery function to be the parent form of those where the data is maintained. Now you may ask me a question that is how about the DataModifiedWithoutSaving function, this function has to be different in every form, and the program should call the one in the child Form not the one in the parent Form. Yes, it is absolutely right and it is just what the Dynamic does. When using Delphi, the virtual and dynamic methods show the property of Dynamic. Virtual and dynamic methods, unlike static methods, can be overridden in descendant classes. When an overridden method is called, the actual (runtime) type of the class or object used in the method call―not the declared type of the variable―determines which implementation to activate.[1] Do remember that the virtual is needed here, because the Delphi accept the method to be static default.[1]
Function DataModifiedWithoutSaving() : Boolean ; virtual; abstract;
Another thing we have to do is move it from private part to the protected part, since the private part can not access out of the class. And I think the implementation part of the function is useless, so I delete this part and declare the function to be abstract. (Note: Since there is a abstract method in the class TForm1, we’d better not make any instant of TForm1. It maybe cause abstract exception[2]).
Now we can inherit a form from this one. The following code is the one we just get.
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Unit1, Grids, DBGrids, StdCtrls, Buttons;
type
TForm2 = class(TForm1)
private
{ Private declarations }
public
{ Public declarations }
protected
{ protected declarations}
function DataModifiedWithoutSaving():Boolean;override;
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
{ TForm2 }
function TForm2.DataModifiedWithoutSaving: Boolean;
begin
//the code should deal with the data in form2.
end;
end.
The DataModifiedWithoutSaving function’s declaration and implementation is not added automatic, we should add them by ourselves. Ok, let’s see how this program works.
At first we get a TFrom2’s object named Form2, and modified some data in it. Then try to close the window, the warning message dialog will appear and tell you the data is modified without saving.
It works just as what we hope. And you can add some more form to test it. Maybe if we use following code to initialize the child form, the Dynamic property of OOP will be showed sufficiently.
Function showForm2() : Boolean;
Var Form2 : TForm1; // note : thought the type is TForm1, but the Dynamic make it
Begin // work well.
Try
Form2 := TForm2.Create(Self);
Form2.ShowModule;
Finally
Form2.Free;
End;
End;
[1] Delphi help: Object Pascal Language Guide.
[2] Delphi help: Component writer‘’s guide
相关文章:
- · ASP整合的一个SQL语句类
- · Jmail发信的实例,模块化随时调用
- · ASP字数计算函数
- · 无刷新随时取得用户当前活动信息
- · ASP自定义函数,仿VBA中域函数DLookup
- · asp中对ip进行过滤限制函数
- · 让ASP程序运行于非Windows平台
- · ASP中实现文件上传方法的研究
- · asp国标转大五码
- · Global.asa文件用法大全
- · 微软建议的ASP性能优化28条守则
- · 在ASP中取得服务器网卡的MAC地址、DNS地址等网络信息
- · ASP中记录的分页
- · 下载网页中的所有资源
- · 函数方便制作管理界面
- · 无限级目录树+记忆节点状态
- · 利用C#在SQL Server2000存取图像 For Window
- · 通过MSXML2自动获取QQ个人头像及在线情况(给初学者)
- · ASP模板类[实现一维循环和二维循环,可以从文件、数据库、变量取摸板]
- · 各种存储过程使用指南
- · 树型结构在ASP中的简单解决
- · 用ASPJPEG组件制作图片的缩略图和加水印
- · 用asp制作强大的搜索引擎(一)
- · VBscript和javascript的选择
- · 在ASP中使用SQL语句之2:用WHERE子句设置查询条件
- · 在线压缩WINRAR文件
- · 在线解压缩上传的WINRAR文件
- · 用javascript实现的日历
- · 在表单里使用”post”和”get”有什么区别
- · 如何把ASP编写成DLL
- · ASP计数器设计详解(转载)
- · 统计在线人数、每日访问人数和总人数
- · 如何动态ASP文件
- · 用asp打开光驱!
- · asp分页显示详论
- · 面向对象的ASP技术:思考与实践
- · 如何获得真实的ip
- · 对文件夹操作2
