override deal with window closing in database application

來源:互聯網
上載者:User
application|window 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 f



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.