override deal with window closing in database application

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

时间: 2024-12-01 00:32:57

override deal with window closing in database application的相关文章

Beginner: Using Servlets to display, insert and update records in database.(3)

servlet Updating records in the Database with Java Servlets. Overview : This article is next in the series of articles about selecting, inserting, updating and deleting records from the database using JDBC. In this article we will learn how to update

Beginner: Using Servlets to display, insert and update records in database.(2)

servlet Inserting records into the Database with Java Servlets. Overview : This article is next in the series of articles about selecting, inserting, updating and deleting records from the database using JDBC. In this article we will learn how to ins

javascript中Window对象的方法属性与生命周期

Window 对象和浏览器窗口1: 一个新文档被装载到窗口或框架中时,那个窗口或框架的 Window 对象就会被重置为默认状态,即由前一个文档中的脚本定义的所有属性和函数都将被清除所有存在过的痕迹.其与前一个此窗口打开的文档无半点瓜葛,出身是绝对的清白.Window 对象和浏览器窗口2: 只要浏览器的顶级窗口存在,那么代表它的Window对象就会一直存在.无论有多少个页面像走城门一样在这个窗口中来来去去,只要浏览器的顶级窗口存在,对它的 Window 对象的引用都有效.这个顶级窗口打开多久,它的

Struts2中操作request,session,application的方法

  Map类型--request,session,application 真实类型--HttpServletRequest,HttpSession,ServletContext Map类型是Struts对真实类型的一个封装,会将真实类型映射到Map类型中   取得上述元素有以下4种方法 1  通过ActionContext来访问request,session,application对象 2  通过实现RequestAware.SessionAware.ApplicationAware接口来访问r

struts2 获得request session application的四种方式

(一)Map(在web.xml中必须使用2.1以上的配置) public class LoginAction1 extends ActionSupport { private Map request;private Map session;private Map application; public LoginAction1() {request = (Map)ActionContext.getContext().get("request");session = ActionCont

【OH】Glossary Oracle词汇表(中)

Glossary [OH]Glossary Oracle词汇表(中) Oracle? Database Net Services Administrator's Guide 11g Release 2 (11.2) E41945-02 Glossary ● access control list (ACL) The group of access directives that you define. The directives grant levels of access to specif

稳扎稳打Silverlight(44)

稳扎稳打Silverlight(44) - 4.0浏览器外运行(Out Of Browser)之OOB的增强及其新增的NotificationWindow 介绍 Silverlight 4.0 OOB 模式的新特性: * 新增了 Closing 事件 * 实现程序在 OOB 模式下的自动更新 * NotificationWindow - 在 OOB 模式下显示通知窗口,也就是 toast * 实现自定义的 NotificationWindow 在线DEMO http://www.cnblogs.

我的Android进阶之旅------>WindowManager.LayoutParams介绍

本文转载于: http://hubingforever.blog.163.com/blog/static/171040579201175111031938/ 本文参照自: http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html http://blog.sina.com.cn/s/blog_4b3c1f950100qd9s.html public static class WindowM

ICEfaces v2.0.1发布 基于Ajax的JSF开发框架

ICEfaces是一个基于Ajax的JSF开发框架.ICEfaces原本是一个商业产品,现已开源基于Mozilla Public License发布.它提供一整套完整的Java EE应用程序开发组件,能够帮助开发人员用纯Java(not JavaScript)快速开发瘦客户端胖互联网应用程序(Rich Internet Applications:RIA).可通过其提供的一个在线Demo体验一下ICEfaces的强大组件. ICEFaces是JSF组件的一个类库,并在此基础上添加了对AJAX特有的