override deal with window closing in database application (轉)
In the database application development, the programmer should often deal with one thing that is when the user close a window (called Foin :namespace prefix = st1 ns = "urn:schemas--com::smarttags" />
unit Unit1;
interface
uses
, 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: T; 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
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
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;overr;
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]
[2]
版權所有:idilent 網站轉載請註明作者 其他轉載方式請與作者聯絡(to:idilent@.com.cn">idilent@yahoo.com.cn)。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-998909/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Qt Application Menu In Window and MacQTAPPMac
- Window Application has "update" key wordsAPP
- How a Database Is Mounted with Real Application Clusters (294)DatabaseAPP
- Types of Oracle Database Users : Application Developers (4)OracleDatabaseAPPDeveloper
- Oracle Database, Application Server, and Collaboration Suite DocumentationOracleDatabaseAPPServerUI
- Types of Oracle Database Users : Application Administrators (5)OracleDatabaseAPP
- Override setterIDE
- 閃回資料庫時間視窗(flashback database window)資料庫Database
- Window安全設定(轉)
- makefile之overrideIDE
- @Override標籤IDE
- Aura QuickAction OverrideUIIDE
- How To Deal With Split Brain In Oracle 19c RacAIOracle
- WPF initialization for opening and unitialization for closing process
- Application(EBS)開發整理(轉)APP
- 方法重寫(Override)IDE
- Oracle Appliactions 11i concepts(三) - Application Database Organization(1)OracleAPPDatabase
- window history pushState replaceState 跳轉原理
- X window遠端登入(轉)
- FC6下的application(轉)APP
- Java中@Override的作用JavaIDE
- JAVA中 @Override 的作用JavaIDE
- 前端效能監控-window.performance(轉)前端ORM
- x-window字型原理及美化(轉)
- keil error:#8:missing closing quote 處理Error
- AC040--Period-End Closing Postings in CO
- Setup Standby Database on One PC(轉)Database
- [轉]How to release space from databaseDatabase
- (轉)Standby Database的工作原理Database
- imp full database (轉官檔)Database
- C#之Abstract、Vitrual、OverrideC#IDE
- Java之所有物件的公用方法>9.Always override hashCode when you override equalsJava物件IDE
- Scheduler in Oracle Database 10g(轉)OracleDatabase
- Elixir 分散式 Application 故障轉移和接管分散式APP
- Web Application 開 發 利 器 - WebSnap(十二) (轉)WebAPP
- 用VC++.net製作WinForm Application (轉)C++ORMAPP
- 使用Shell.Application技術之二 (轉)APP
- Web Application 開 發 利 器 - WebSnap(四) (轉)WebAPP