Override deal with Windows closing in database application

Source: Internet
Author: User
Tags abstract copy one table
Application|window in the database application development, the programmer should often and deal with one thing ' is ' when t He user close a Windows (called Form in Delphi) where data is maintained, the program should judge whether the data is CH Anged without saving and warn the user about this case. Almost the programmer can deal with this easily, and the code are very simple as we know. The following code (written in Delphi 6) was 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 (nam Ed Dbeditname) which can be used to edit the data of the table and six Buttons (respective named Btnadd, Btnmodify, Btndel Ete, Btnsave, Btncancle, btnclose). The last button was used to close the Form and the others are dealing with the data. I am their name show their function clearly and no explanation are 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 is changed without saving, would you like save the data? ', mtwarning,[mbyes,mbno],0) = Mrno the N

Begin

Canclose: = True;

End ELSE begin

Btnsaveclick (Self);

Canclose: = True;

End

End

End

Procedure Tform1.btnsaveclick (Sender:tobject);

Begin

//

End

End.



The above code isn't very good (maybe can give a more effect one), but it really can deal with the problem we mention Ed 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 are it is a boring thing? Maybe not, but how about this team leader does not have the code you have written are very good and he gives to you another One? Then copy again? I am The It is needed to find a effect method to deal with this problem. The tool I show the example are 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 them) are the tools which support OOP (object-oriented
Programming). The three main property of the OOP are encapsulation, inheritance and Dynamic. We can use the ' Dynamic ' to deal with the problem. To does so, we need a form which has the Formclosequery function to being the parent form of those where the data is maintained . Now I ask I a question that's 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 "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 is overridden in descendant classes. When a overridden is called, the actual (runtime) type of the class or object used in the method call―not the Decl Ared type of the variable―determines which implementation to activate. [1] Do remember this virtual is needed here, because the Delphi accept The method is static default. [1]



Function datamodifiedwithoutsaving (): Boolean; Virtual Abstract



Another thing we have to do are move it from private part to the protected part, since the private part can don't access out of the class. And I the implementation part of the "function is useless", so I deletes this part and declare the F



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.