首先创建一个窗体,放入ActionList1控件,命名为actlstChild,放入ImageList1,命名为actlstChildImageList,再设置一些基本的属性,如:FormStyle为fsMDIChild,窗体最大化等。此窗体用于将来其它窗体的基窗体。当操作主窗体中的一些功能时需要关联到基窗体上。因此窗体创建的时候要将主窗体的操作事件全部赋值到基窗体。为了让界面和代码分离,我们需要创建一个类(单元文件),专门用于基窗体上的一些操作。新建单元文件untBaseClass.pas,代码如下:
unit untBaseClass;
interface
uses
Windows,Messages,SysUtils,Variants,Classes,DB,ADODB;
type
TBaseClass = class
private
public
constructor create;
destructor Destroy;override;
procedure AddData;
procedure CancelData;
procedure SaveData;
procedure DeleteData;
procedure ModifyData;
end;
implementation
uses untDM, untGlobal, untMain, untBase;
constructor TBaseClass.create;
begin
end;
destructor TBaseClass.Destroy;
begin
inherited;
end;
procedure TBaseClass.AddData;
begin
With frmDM do
if Assigned(dsMaster.DataSet) then
if not(dsMaster.State in [dsEdit,dsInsert]) then
begin
try
(dsMaster.DataSet as TADOQuery).Append;
Except
on E:exception do
MsgBox('增加错误!'+#13+'出错数类名:'+E.ClassName+#13+'出错信息:'+E.Message,0);
end;
end;
end;
procedure TBaseClass.CancelData;
begin
MsgBox('取消操作',0)
end;
procedure TBaseClass.DeleteData;
begin
end;
procedure TBaseClass.SaveData;
begin
With frmDM do
if Assigned(dsMaster.DataSet) then
if dsMaster.State in [dsEdit,dsInsert] then
begin
try
conDB.BeginTrans;
(dsMaster.DataSet as TADOQuery).Post;
(dsMaster.DataSet as TADOQuery).UpdateBatch;
conDB.CommitTrans;
Except
conDB.RollbackTrans;
Raise exception.Create('增加错误!'
end;
end;
end;
procedure TBaseClass.ModifyData;
begin
MsgBox('修改数据操作',0)
end;
end.
此时将基窗体的事件关联起来,基窗体的主要功能代码如下:
procedure TfrmBase.FormCreate(Sender: TObject);
begin
objBaseClass:=TBaseClass.create;
RelationMainFormEvent;
end;
procedure TfrmBase.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action:=caFree;
Self:=nil;
end;
procedure TfrmBase.RelationMainFormEvent;
begin
frmMain.actAdd.OnExecute:=actAdd.OnExecute;
frmMain.actAdd.Enabled:=True;
frmMain.actModify.OnExecute:=actModify.OnExecute;
frmMain.actModify.Enabled:=True;
frmMain.actSave.OnExecute:=actSave.OnExecute;
frmMain.actSave.Enabled:=true;
end;
procedure TfrmBase.actAddExecute(Sender: TObject);
begin
objBaseClass.AddData;
end;
procedure TfrmBase.actDeleteExecute(Sender: TObject);
begin
objBaseClass.DeleteData;
end;
procedure TfrmBase.actSaveExecute(Sender: TObject);
begin
objBaseClass.SaveData;
end;
procedure TfrmBase.actCancelExecute(Sender: TObject);
begin
objBaseClass.CancelData;
end;
procedure TfrmBase.actModifyExecute(Sender: TObject);
begin
objBaseClass.ModifyData;
end;
基本的功能做完后,下一步就可以创建一个新窗体并继承此窗体。