打印本文 打印本文 关闭窗口 关闭窗口
override deal with window closing in database application
作者:武汉SEO闵涛  文章来源:敏韬网  点击数2037  更新时间:2009/4/23 18:38:05  文章录入:mintao  责任编辑:mintao

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;

[1] [2] [3]  下一页

打印本文 打印本文 关闭窗口 关闭窗口