In The Database Application Development, The Programmer SHOULD OFTEN DEAL WITH 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;
BTNDLETE: 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}
Boolean; Virtual; Abstract;
END;
Var Form1: TFORM1;
IMPLEMENTATION
{$ R * .dfm}
Procedure TFORM1.FORMCLOSEQUERY (Sender: TOBJECT)
Var canclose: boolean;
Begin
IF datamodifiedwithoutsaving the
Begin
IF Messagedlg ('The Data Has Be Changd Saving, Would You Like Save The Data?', MTWARNING, [MBYES, MBNO], 0) = mRNO Thenbegin
CANCLOSE: = True;
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 her
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-orientedprogramming). 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 Uings
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 ;
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 May Cause Abstract Exception [2]).
Now We can inherit a form from this one. The folload code is the one wewear 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 will dress DEAL with the data in form2.
END;
End.
The DatamodifiedWithoutSaving Function's Declaration and Implementation IS Not Added Automatic, We Should AUTOMATIC, We Should Add..
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
Copyright: IDILENT website reproduced please indicate the author's other reprint, please contact the author (iDilent@yahoo.com.cn).