www.pudn.com > PrintServer.rar > EnterDeptInfo.pas, change:2006-02-23,size:7746b


unit EnterDeptInfo; 
 
interface 
 
uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, ExtCtrls, StdCtrls, Buttons, DB, MemDS, DBAccess, MSAccess, 
  Grids, DBGridEh, ComCtrls; 
 
type 
  TEnterDeptInfoForm = class(TForm) 
    GroupBox1: TGroupBox; 
    StatusBar1: TStatusBar; 
    Panel1: TPanel; 
    DBGridEh1: TDBGridEh; 
    Label2: TLabel; 
    Edit1: TEdit; 
    DataSource1: TDataSource; 
    DataQuery: TMSQuery; 
    WorkQuery: TMSQuery; 
    BitBtn1: TBitBtn; 
    BitBtn3: TBitBtn; 
    BitBtn4: TBitBtn; 
    Bevel3: TBevel; 
    procedure FormClose(Sender: TObject; var Action: TCloseAction); 
    procedure FormCreate(Sender: TObject); 
    procedure BitBtn4Click(Sender: TObject); 
    procedure BitBtn1Click(Sender: TObject); 
    procedure DBGridEh1KeyDown(Sender: TObject; var Key: Word; 
      Shift: TShiftState); 
    procedure FormKeyDown(Sender: TObject; var Key: Word; 
      Shift: TShiftState); 
    procedure BitBtn1MouseMove(Sender: TObject; Shift: TShiftState; X, 
      Y: Integer); 
    procedure BitBtn2MouseMove(Sender: TObject; Shift: TShiftState; X, 
      Y: Integer); 
    procedure BitBtn3MouseMove(Sender: TObject; Shift: TShiftState; X, 
      Y: Integer); 
    procedure BitBtn4MouseMove(Sender: TObject; Shift: TShiftState; X, 
      Y: Integer); 
    procedure Edit1Exit(Sender: TObject); 
    procedure Edit1KeyPress(Sender: TObject; var Key: Char); 
    procedure BitBtn2Click(Sender: TObject); 
    procedure BitBtn3Click(Sender: TObject); 
    procedure DataQueryBeforeInsert(DataSet: TDataSet); 
  private 
    NewFlag: Boolean; 
    EditFlag: Boolean; 
  public 
    { Public declarations } 
  end; 
 
var 
  EnterDeptInfoForm: TEnterDeptInfoForm; 
 
implementation 
 
{$R *.dfm} 
 
procedure TEnterDeptInfoForm.FormClose(Sender: TObject; var Action: TCloseAction); 
begin 
  Action := CaFree; 
end; 
 
procedure TEnterDeptInfoForm.FormCreate(Sender: TObject); 
begin 
  NewFlag := False; 
  EditFlag := False; 
end; 
 
procedure TEnterDeptInfoForm.BitBtn4Click(Sender: TObject); 
begin 
  Close; 
end; 
 
procedure TEnterDeptInfoForm.BitBtn1Click(Sender: TObject); 
begin 
  if (NewFlag = True) then 
  begin 
    ShowMessage('您已新建了一条记录, 但没有完成, 请先完成本记录再进行新建!'); 
    Abort; 
  end 
  else 
    if (EditFlag = True) then 
  begin 
    if (Application.MessageBox('你正在新增记录,是否取消新建进行修改?', '系统提示', MB_YESNO + MB_ICONWARNING) = 6) then 
    begin 
      GroupBox1.Enabled := True; 
      DBGridEh1.Enabled := True; 
      NewFlag := True; 
      EditFlag := False; 
      Edit1.Clear; 
      Edit1.ReadOnly := False; 
      Edit1.SetFocus; 
    end 
    else 
    begin 
      Abort; 
    end; 
  end 
  else 
  begin 
    NewFlag := True; 
    GroupBox1.Enabled := True; 
    DBGridEh1.Enabled := True; 
    Edit1.Clear; 
    Edit1.ReadOnly := False; 
    Edit1.SetFocus; 
  end; 
end; 
 
procedure TEnterDeptInfoForm.DBGridEh1KeyDown(Sender: TObject; var Key: Word; 
  Shift: TShiftState); 
begin 
  if (Key = VK_delete) then 
  begin 
    if (Application.MessageBox('真的要删除选中的记录吗?', '系统提示', MB_YESNO + MB_ICONWARNING) = 6) then 
    begin 
      DataQuery.Edit; 
      DataQuery.Delete; 
      DataQuery.Active := False; 
      DataQuery.Active := True; 
    end 
    else 
    begin 
      Abort; 
    end; 
  end; 
end; 
 
procedure TEnterDeptInfoForm.FormKeyDown(Sender: TObject; var Key: Word; 
  Shift: TShiftState); 
begin 
  if (Key = VK_Escape) then 
  begin 
    Edit1.Clear; 
    GroupBox1.Enabled := False; 
    NewFlag := False; 
    EditFlag := False; 
    DBGridEh1.Enabled := True; 
  end; 
  //按下CTRL+N时进行新增操作 
  if (Shift = [ssCtrl]) then 
  begin 
    if (Key = 78) then 
    begin 
      BitBtn1.Click; 
    end; 
  end; 
  //按下CTRL+S时进行保存操作 
  if (Shift = [ssCtrl]) then 
  begin 
    if (Key = 83) then 
    begin 
      BitBtn3.Click; 
    end; 
  end; 
end; 
 
procedure TEnterDeptInfoForm.BitBtn1MouseMove(Sender: TObject; Shift: TShiftState; 
  X, Y: Integer); 
begin 
  StatusBar1.Panels[1].Text := '按下本按钮,将新增一条新记录'; 
end; 
 
procedure TEnterDeptInfoForm.BitBtn2MouseMove(Sender: TObject; Shift: TShiftState; 
  X, Y: Integer); 
begin 
  StatusBar1.Panels[1].Text := '按下本按钮,将修改选中的记录'; 
end; 
 
procedure TEnterDeptInfoForm.BitBtn3MouseMove(Sender: TObject; Shift: TShiftState; 
  X, Y: Integer); 
begin 
  StatusBar1.Panels[1].Text := '按下本按钮,将保存一条记录'; 
end; 
 
procedure TEnterDeptInfoForm.BitBtn4MouseMove(Sender: TObject; Shift: TShiftState; 
  X, Y: Integer); 
begin 
  StatusBar1.Panels[1].Text := '按下本按钮将会关闭当前窗口'; 
end; 
 
procedure TEnterDeptInfoForm.Edit1Exit(Sender: TObject); 
var 
  SQL_Str: string; 
begin 
  if (NewFlag = True) then 
  begin 
    if (Trim(Edit1.Text) <> '') then 
    begin 
      SQL_Str := 'Select * From 部门信息表  Where (部门名称='#39 + Edit1.Text + #39 + ')'; 
      WorkQuery.Active := False; 
      WorkQuery.SQL.Clear; 
      WorkQuery.SQL.Add(SQL_Str); 
      WorkQuery.Open; 
      if (WorkQuery.RecordCount > 0) then 
      begin 
        ShowMessage('该部门信息已经在系统中定义过!'); 
        Edit1.Clear; 
        Edit1.SetFocus; 
        Abort; 
      end; 
    end; 
  end; 
end; 
 
procedure TEnterDeptInfoForm.Edit1KeyPress(Sender: TObject; var Key: Char); 
begin 
  if (Key = #13) then 
  begin 
    BitBtn3.SetFocus; 
  end; 
end; 
 
procedure TEnterDeptInfoForm.BitBtn2Click(Sender: TObject); 
begin 
  if (DataQuery.RecordCount <= 0) then 
  begin 
    ShowMessage('没有可以修改的记录,您的操作是错误的!'); 
    Abort; 
  end 
  else 
  begin 
    if (NewFlag = True) then 
    begin 
      if (Application.MessageBox('你正在新增记录,是否取消新增进行修改?', '系统提示', MB_YESNO + MB_ICONWARNING) = 6) then 
      begin 
        EditFlag := True; 
        NewFlag := False; 
        GroupBox1.Enabled := True; 
        DBGridEh1.Enabled := False; 
        Edit1.Clear; 
        Edit1.Text := DataQuery.Fields[0].AsString; 
        Edit1.SetFocus; 
      end 
      else 
      begin 
        Abort; 
      end; 
    end; 
    if (EditFlag = True) then 
    begin 
      if (Application.MessageBox('你正在修改记录,是否取消当前修改?', '系统提示', MB_YESNO + MB_ICONWARNING) = 6) then 
      begin 
        EditFlag := True; 
        NewFlag := False; 
        GroupBox1.Enabled := True; 
        DBGridEh1.Enabled := False; 
        Edit1.Clear; 
        Edit1.Text := DataQuery.Fields[0].AsString; 
        Edit1.SetFocus; 
      end 
      else 
      begin 
        Abort; 
      end; 
    end; 
    EditFlag := True; 
    NewFlag := False; 
    GroupBox1.Enabled := True; 
    DBGridEh1.Enabled := False; 
    Edit1.Clear; 
    Edit1.Text := DataQuery.Fields[0].AsString; 
    Edit1.SetFocus; 
  end; 
end; 
 
procedure TEnterDeptInfoForm.BitBtn3Click(Sender: TObject); 
begin 
  if (GroupBox1.Enabled = False) then 
  begin 
    ShowMessage('您没有新建记录或修改记录, 无需进行保存操作!'); 
    Abort; 
  end 
  else 
  begin 
    if (NewFlag = True) then 
    begin 
      if (Trim(Edit1.Text) = '') then 
      begin 
        ShowMessage('颜色为浅蓝色的输入栏是必填项, 请填入相应的信息然后再进行保存!'); 
        Edit1.SetFocus; 
        Abort; 
      end 
      else 
      begin 
        DataQuery.Active := True; 
        DataQuery.Append; 
        DataQuery.Fields[0].AsString := Edit1.Text; 
        DataQuery.Post; 
        Edit1.Clear; 
        GroupBox1.Enabled := False; 
        NewFlag := False; 
        BitBtn1.SetFocus; 
      end; 
    end; 
  end 
end; 
 
procedure TEnterDeptInfoForm.DataQueryBeforeInsert(DataSet: TDataSet); 
begin 
  if (Self.ActiveControl = DBGridEh1) then 
  begin 
    Abort; 
  end; 
end; 
 
end.