分享
 
 
 

override deal with window closing in database application

王朝delphi·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

In the database application development, the programmer should often deal with one thing that is when the user close a window (called Form in Delphi) where data was maintained, the program should judge whether the data was changed without saving and warn the user about this case. Almost all the programmer can deal with this easily, and the code is very simple as we know. The following code (written in Delphi 6) is the normal method to deal with the problem. It is base on one table maintenance and there are a DBGrid (named DBGRid) which shows the data of the table, a DBEdit (named DBEditName) which can be used to edit the data of the table and six Buttons (respective named BtnAdd, BtnModify, BtnDelete, BtnSave, BtnCancle, BtnClose). The last button is used to close the Form and the others are dealing with the data. I think their name show their function clearly and no explanation is given here.

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

版权所有:idilent 网站转载请注明作者 其他转载方式请与作者联系(idilent@yahoo.com.cn)。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有