⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 base_detail.pas

📁 一个MRPII系统源代码版本
💻 PAS
字号:
unit Base_Detail;

Interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Base_Dialog, StdCtrls, Db, AdODB, ExtCtrls, Mask, DBCtrls, ExtEdit;

Type
  TFrm_Base_Detail = Class(TFrm_Base_Dialog)
    Pnl_Add: TPanel;
    procedure FormActivate(Sender: TObject);
    procedure AllChange(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure btn_okClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);   
    procedure OnEnter(Sender: TObject);
  private
    { Private declarations }
    EnableControls:String;    
    procedure Save;
  protected
    { protected declarations }   
    ShowFlag{窗口弹出时ShowFlag=False},Changed{控件有改动时=True}:Boolean;
    FormCaption:String;//不推荐
    ExtendCaption:Boolean;//不推荐
    Status:String;//标识当前状况'Add','Edit','ReadOnly'
    SetFocus_Control:TWinControl;//需要聚焦的控件
    procedure InitControls; virtual;//初始化控件
    procedure SaveData; virtual;//保存数据
    //根据状态,设置可用控件
    procedure SetStatus(CurrentStatus:String;var EnableControls:String); virtual;
  public
    { Public declarations }
    AdoQry_Maintain:TAdoQuery;//需要维护的表控件的引用
    Add:Boolean;//不推荐
    ReadOnly:Boolean;//不推荐
    procedure InitForm(AdOConnection:TAdOConnection;FormStatus:String;
      AdoQry_Main:TAdoQuery);virtual;
  end;

var
  Frm_Base_Detail: TFrm_Base_Detail;

implementation

uses Sys_Global;

{$R *.DFM}

procedure TFrm_Base_Detail.FormActivate(Sender: TObject);
begin
  if ReadOnly then
    Status:='ReadOnly'
  else if Add then
    Status:='Add'
  else
    Status:='Edit';
  ShowFlag:=False;
  InitControls;
  ShowFlag:=True;
  btn_ok.Enabled:=False;
  inherited;
end;

procedure TFrm_Base_Detail.AllChange(Sender: TObject);
begin//缺省的onChange事件处理过程
  inherited;
  btn_ok.Enabled:=True;
  Changed:=True;
end;

procedure TFrm_Base_Detail.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin//处理pageup,pagedown按键
  inherited;
  if(Status<>'Add')then
    if(Key=VK_PRIOR)or(Key=VK_NEXT)then
    begin
      if(btn_ok.Enabled)and(DispInfo('数据有改动,需要保存吗?',2)='y')then
      begin
        Changed:=False;
        inherited btn_okClick(Sender);
        Save;
      end;
      if Key=VK_PRIOR then
        AdoQry_Maintain.Prior
      else
        AdoQry_Maintain.Next;
      InitControls;
      Btn_ok.Enabled:=False;
      Key:=0;
    end;
end;

procedure TFrm_Base_Detail.btn_okClick(Sender: TObject);
begin
  Changed:=False;
  inherited;
  Save;
  if Status='Add' then
  begin
    DispInfo('当前数据已经保存,可以继续增加!',3);
    InitControls;
    btn_ok.Enabled:=False;
  end
  else
    ModalResult:=mrOk;
end;

procedure TFrm_Base_Detail.InitControls;
var
  i:Integer;
  Control:TControl;
  AcControl:TWinControl;
  NotifyEvent:TNotifyEvent;
begin//给窗体上控件赋值时可重载本过程
  if GetOnExitEvent(ActiveControl,NotifyEvent) then
    Control:=ActiveControl
  else
    Control:=nil;
  AcControl:=ActiveControl;
  ActiveControl:=btn_Cancel;
  EnableControls:='';
  SetStatus(Status,EnableControls);
  if Status='ReadOnly' then
  begin
    for i:=0 to ControlCount-1 do
    begin
      if(not(Controls[i] is TLabel))and
        (not(Controls[i] is TPanel))and
        (not(Controls[i] is TButton))then
        Controls[i].Enabled:=False;
    end;
  end
  else if EnableControls<>'' then
  begin
    for i:=0 to ControlCount-1 do
    begin
      if(not(Controls[i] is TLabel))and
        (not(Controls[i] is TPanel))and
        (not(Controls[i] is TButton))and
        (Pos(UpperCase(Controls[i].Name)+',',UpperCase(EnableControls))=0)then
        Controls[i].Enabled:=False
      else
        Controls[i].Enabled:=True;
    end;
  end;
  if (AcControl<>nil)and(AcControl.Enabled) then
    ActiveControl:=AcControl;
  if((Status='Add')or(not ShowFlag))and(SetFocus_Control<>nil)
    and(SetFocus_Control.Enabled) then
    SetFocus_Control.SetFocus;
  if Control<>nil then
    SetOnExitEvent(Control,NotifyEvent);
  if FormCaption='' then
    FormCaption:=Caption;
  if Status='ReadOnly' then
  begin
    if ExtendCaption then
      Caption:=FormCaption+'-[查询]';
//    Pnl_Add.Caption:='查询';
  end
  else if Status='Add' then
  begin
    if ExtendCaption then
      Caption:=FormCaption+'-[新增]';
   // Pnl_Add.Caption:='增加';
  end
  else
  begin
    if ExtendCaption then
      Caption:=FormCaption+'-[修改]';
  //  Pnl_Add.Caption:='修改';
  end;
end;

procedure TFrm_Base_Detail.Save;
begin//数据保存过程
  if(Status='Add')then
  begin
    AdoQry_Maintain.Append;
  end
  else
  begin
    AdoQry_Maintain.Edit;
  end;
  try
    SaveData;
  except
    DispInfo('数据保存出现错误!'+#13+#10+'可能是该记录已经被其他数据引用!',1);
    Abort;
  end;
end;

procedure TFrm_Base_Detail.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin//
  inherited;
  if AdoQry_Maintain.State=dsInsert then
    AdoQry_Maintain.Cancel
  else if AdoQry_Maintain.State=dsEdit then
    AdoQry_Maintain.Post;
end;

procedure TFrm_Base_Detail.SaveData;
begin

end;

procedure TFrm_Base_Detail.FormCreate(Sender: TObject);
var
  i:Integer;
begin//定义缺省onexit,onChange事件处理函数
  inherited;
  ReadOnly:=False;
  ExtendCaption:=True;
  for i:=0 to ControlCount-1 do
  begin
    SetOnChangeEvent(Controls[i],AllChange);
    SetOnEnterEvent(Controls[i],OnEnter);
  end;
end;

procedure TFrm_Base_Detail.SetStatus(CurrentStatus: String;
  var EnableControls: String);
begin
  EnableControls:='';
end;

procedure TFrm_Base_Detail.InitForm(AdOConnection: TAdOConnection;
  FormStatus: String; AdoQry_Main: TAdoQuery);
begin//定义数据库连接,窗体状态
  SetDBConnect(AdOConnection);
  Status:=FormStatus;
  AdoQry_Maintain:=AdoQry_Main;
  ReadOnly:=False;
  if Status='Add' then
    Add:=True
  else if Status='Edit' then
    Add:=False
  else
    ReadOnly:=True;
end;

procedure TFrm_Base_Detail.OnEnter(Sender: TObject);
begin
  Changed:=False;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -