📄 untbasemdedit.pas
字号:
unit untBaseMDEdit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, untBaseDialog, StdCtrls, Buttons, ExtCtrls, jpeg, DB, ADODB,
Grids, DBGrids, untGlobalVar, DBCtrls, fcButton, fcImgBtn;
type
TfrmBaseMDEdit = class(TfrmBaseDialog)
pnlUp: TPanel;
grdDetail: TDBGrid;
adsDetail: TADODataSet;
dsMaster: TDataSource;
dsDetail: TDataSource;
btnDelDetail: TfcImageBtn;
procedure btnDelDetailClick(Sender: TObject);
procedure btnOKClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure dsMasterDataChange(Sender: TObject; Field: TField);
procedure dsDetailDataChange(Sender: TObject; Field: TField);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure adsDetailAfterDelete(DataSet: TDataSet);
procedure adsDetailAfterInsert(DataSet: TDataSet);
procedure adsDetailBeforePost(DataSet: TDataSet);
private
FDataSet: TADODataSet;
FOpMode: TOperatorMode;
FModified: Boolean;
procedure SetDataSet(const Value: TADODataSet);
procedure SetOpMode(const Value: TOperatorMode);
procedure SaveRecord;
{ Private declarations }
protected
procedure CheckAvoild; virtual;
function CheckDataValid: Boolean; virtual;
procedure DoBeforePost; virtual;
procedure AfterPost; virtual;
procedure AfterFormShow; virtual;
procedure InitComponents; virtual;
public
{ Public declarations }
ContinueAppend: Boolean;
property DataSet: TADODataSet read FDataSet write SetDataSet;
property OpMode: TOperatorMode read FOpMode write SetOpMode;
property Modified: Boolean read FModified write FModified;
end;
var
frmBaseMDEdit: TfrmBaseMDEdit;
implementation
uses untGlobalFun;
{$R *.dfm}
procedure TfrmBaseMDEdit.AfterPost;
begin
end;
procedure TfrmBaseMDEdit.btnDelDetailClick(Sender: TObject);
begin
inherited;
if (not adsDetail.Active) or (adsDetail.RecordCount=0) then
Exit;
if adsDetail.State = dsInsert then
adsDetail.Cancel
else
adsDetail.Delete;
end;
function TfrmBaseMDEdit.CheckDataValid: Boolean;
begin
Result := true;
end;
procedure TfrmBaseMDEdit.DoBeforePost;
begin
end;
procedure TfrmBaseMDEdit.SetDataSet(const Value: TADODataSet);
begin
FDataSet := Value;
dsMaster.DataSet := Value;
end;
procedure TfrmBaseMDEdit.SetOpMode(const Value: TOperatorMode);
var
i: Integer;
begin
FOpMode := Value;
for i := 0 to pnlUp.ControlCount - 1 do
begin
if pnlUp.Controls[i] is TDBEdit then
begin
TDBEdit(pnlUp.Controls[i]).ReadOnly := (Value in [omDel,omBrowse]);
if TDBEdit(pnlUp.Controls[i]).ReadOnly then
TDBEdit(pnlUp.Controls[i]).Color := clActiveBorder
else
TDBEdit(pnlUp.Controls[i]).Color := clWindow;
end;
if pnlUp.Controls[i] is TDBMemo then
begin
if (pnlUp.Controls[i].Tag = 0) then
begin
TDBMemo(pnlUp.Controls[i]).ReadOnly := (Value in [omDel,omBrowse]);
if TDBMemo(pnlUp.Controls[i]).ReadOnly then
TDBMemo(pnlUp.Controls[i]).Color := clActiveBorder
else
TDBMemo(pnlUp.Controls[i]).Color := clWindow;
end;
end;
if pnlUp.Controls[i] is TDBListBox then
begin
TDBListBox(pnlUp.Controls[i]).ReadOnly := (Value in [omDel,omBrowse]);
if TDBListBox(pnlUp.Controls[i]).ReadOnly then
TDBListBox(pnlUp.Controls[i]).Color := clActiveBorder
else
TDBListBox(pnlUp.Controls[i]).Color := clWindow;
end;
if pnlUp.Controls[i] is TDBComboBox then
TDBComboBox(pnlUp.Controls[i]).Enabled := not (Value in [omDel,omBrowse]);
if pnlUp.Controls[i] is TDBCheckBox then
TDBCheckBox(pnlUp.Controls[i]).Enabled := not (Value in [omDel,omBrowse]);
if pnlUp.Controls[i] is TDBRadioGroup then
TDBRadioGroup(pnlUp.Controls[i]).Enabled := not (Value in [omDel,omBrowse]);
if pnlUp.Controls[i] is TDBLookupListBox then
begin
TDBLookupListBox(pnlUp.Controls[i]).ReadOnly := (Value in [omDel,omBrowse]);
if TDBLookupListBox(pnlUp.Controls[i]).ReadOnly then
TDBLookupListBox(pnlUp.Controls[i]).Color := clActiveBorder
else
TDBLookupListBox(pnlUp.Controls[i]).Color := clWindow;
end;
if pnlUp.Controls[i] is TDBLookupComboBox then
TDBLookupComboBox(pnlUp.Controls[i]).Enabled := not (Value in [omDel,omBrowse]);
if pnlUp.Controls[i] is TDBRichEdit then
begin
TDBRichEdit(pnlUp.Controls[i]).ReadOnly := (Value in [omDel,omBrowse]);
if TDBRichEdit(pnlUp.Controls[i]).ReadOnly then
TDBRichEdit(pnlUp.Controls[i]).Color := clActiveBorder
else
TDBRichEdit(pnlUp.Controls[i]).Color := clWindow;
end;
end;
if Value in [omDel, omBrowse] then
grdDetail.Options := grdDetail.Options - [dgEditing]
else
grdDetail.Options := grdDetail.Options + [dgEditing];
end;
procedure TfrmBaseMDEdit.btnOKClick(Sender: TObject);
begin
inherited;
SaveRecord;
end;
procedure TfrmBaseMDEdit.SaveRecord;
begin
CheckAvoild;
if not CheckDataValid then exit;
try
DataSet.Connection.BeginTrans;
DoBeforePost;
if DataSet.State in [dsInsert, dsEdit] then DataSet.Post;
if Modified then DataSet.Refresh;
if adsDetail.State in [dsInsert, dsEdit] then adsDetail.Post;
adsDetail.UpdateBatch;
AfterPost;
DataSet.Connection.CommitTrans;
FModified := false;
MsgOk('数据更新成功!');
if ContinueAppend and (OpMode = omNew) then begin
DataSet.Append;
FormShow(Self);
end else
ModalResult := mrOK;
except
DataSet.Connection.RollbackTrans;
MsgOK('保存数据出错!');
end;
end;
procedure TfrmBaseMDEdit.CheckAvoild;
var
i: Integer;
strDispName: string;
begin
for i:=0 to pnlUp.ControlCount-1 do
if pnlUp.Controls[i].Tag = 900 then begin
if pnlUp.Controls[i] is TDBEdit then
if Trim(TDBEdit(pnlUp.Controls[i]).Text)='' then begin
TDBEdit(pnlUp.Controls[i]).SetFocus;
strDispName := DataSet.FieldByName(TDBEdit(pnlUp.Controls[i]).DataField).DisplayLabel;
raise Exception.Create(strDispName+'不能为空!');
end;
if pnlUp.Controls[i] is TDBMemo then
if Trim(TDBMemo(pnlUp.Controls[i]).Text)='' then begin
TDBMemo(pnlUp.Controls[i]).SetFocus;
strDispName := DataSet.FieldByName(TDBMemo(pnlUp.Controls[i]).DataField).DisplayLabel;
raise Exception.Create(strDispName+'不能为空!');
end;
if pnlUp.Controls[i] is TDBComboBox then
if Trim(TDBComboBox(pnlUp.Controls[i]).Text)='' then begin
TDBComboBox(pnlUp.Controls[i]).SetFocus;
strDispName := DataSet.FieldByName(TDBComboBox(pnlUp.Controls[i]).DataField).DisplayLabel;
raise Exception.Create(strDispName+'不能为空!');
end;
if pnlUp.Controls[i] is TDBLookupComboBox then
if Trim(TDBLookupComboBox(pnlUp.Controls[i]).Text)='' then begin
TDBLookupComboBox(pnlUp.Controls[i]).SetFocus;
strDispName := DataSet.FieldByName(TDBLookupComboBox(pnlUp.Controls[i]).DataField).DisplayLabel;
raise Exception.Create(strDispName+'不能为空!');
end;
if pnlUp.Controls[i] is TDBRichEdit then
if Trim(TDBRichEdit(pnlUp.Controls[i]).Text)='' then begin
TDBRichEdit(pnlUp.Controls[i]).SetFocus;
strDispName := DataSet.FieldByName(TDBRichEdit(pnlUp.Controls[i]).DataField).DisplayLabel;
raise Exception.Create(strDispName+'不能为空!');
end;
end;
end;
procedure TfrmBaseMDEdit.FormShow(Sender: TObject);
begin
inherited;
case OpMode of
omNew : begin
Caption := Title + '—新增';
btnOK.Show;
btnCancel.Caption := '取消';
end;
omModi: begin
Caption := Title + '—编辑';
btnOK.Show;
btnCancel.Caption := '取消';
end;
omDel : begin
Caption := Title + '—删除';
btnOK.Show;
btnCancel.Caption := '取消';
end;
omBrowse: begin
Caption := Title + '—查看';
btnOK.Hide;
btnCancel.Caption := '关闭';
end;
end;
FModified := false;
AfterFormShow;
InitComponents;
end;
procedure TfrmBaseMDEdit.dsMasterDataChange(Sender: TObject;
Field: TField);
begin
inherited;
if Field <> nil then
FModified := true;
end;
procedure TfrmBaseMDEdit.dsDetailDataChange(Sender: TObject;
Field: TField);
begin
inherited;
if (Field <> nil) and (not FModified) then
FModified := true;
end;
procedure TfrmBaseMDEdit.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
inherited;
if FModified then
if not MsgQuestion('数据已更改,取消吗?') then
Action := caNone;
end;
procedure TfrmBaseMDEdit.AfterFormShow;
begin
end;
procedure TfrmBaseMDEdit.InitComponents;
begin
end;
procedure TfrmBaseMDEdit.adsDetailAfterDelete(DataSet: TDataSet);
begin
inherited;
Modified := true;
end;
procedure TfrmBaseMDEdit.adsDetailAfterInsert(DataSet: TDataSet);
begin
inherited;
Modified := true;
end;
procedure TfrmBaseMDEdit.adsDetailBeforePost(DataSet: TDataSet);
var
i: Integer;
begin
inherited;
for i:=0 to DataSet.FieldCount-1 do
if DataSet.Fields[i].Visible and (DataSet.Fields[i].Tag = 900) then
if DataSet.Fields[i].IsNull or (Trim(DataSet.Fields[i].AsString) = '') then
begin
DataSet.Fields[i].FocusControl;
raise Exception.Create(DataSet.Fields[i].DisplayLabel+'不能为空!');
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -