www.pudn.com > PrintServer.rar > EnterEmployee.pas, change:2006-02-26,size:11761b


unit EnterEmployee; 
 
interface 
 
uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, DB, MemDS, DBAccess, MSAccess, Grids, DBGridEh, StdCtrls, 
  NumEdit, ExtCtrls, cxControls, cxContainer, cxEdit, cxTextEdit, 
  cxMaskEdit, cxDropDownEdit, cxLookupEdit, cxDBLookupEdit, 
  cxDBLookupComboBox, Buttons, ComCtrls, IPEdit; 
 
type 
  TEnterEmployeeForm = class(TForm) 
    GroupBox1: TGroupBox; 
    Panel1: TPanel; 
    StatusBar1: TStatusBar; 
    BitBtn1: TBitBtn; 
    BitBtn2: TBitBtn; 
    BitBtn3: TBitBtn; 
    BitBtn4: TBitBtn; 
    Label1: TLabel; 
    Combobox1: TcxLookupComboBox; 
    Label2: TLabel; 
    Edit1: TEdit; 
    Label3: TLabel; 
    Edit2: TEdit; 
    DBGridEh1: TDBGridEh; 
    DataSource1: TDataSource; 
    DataSource2: TDataSource; 
    SortQuery: TMSQuery; 
    DataQuery: TMSQuery; 
    WorkQuery: TMSQuery; 
    Bevel2: TBevel; 
    Bevel3: TBevel; 
    Bevel4: TBevel; 
    Bevel5: TBevel; 
    Bevel6: TBevel; 
    Label4: TLabel; 
    Bevel1: TBevel; 
    Bevel7: TBevel; 
    IPEdit1: TIPEdit; 
    procedure Edit1Exit(Sender: TObject); 
    procedure Combobox1KeyPress(Sender: TObject; var Key: Char); 
    procedure Edit1KeyPress(Sender: TObject; var Key: Char); 
    procedure Edit2KeyPress(Sender: TObject; var Key: Char); 
    procedure BitBtn1Click(Sender: TObject); 
    procedure BitBtn2Click(Sender: TObject); 
    procedure BitBtn3Click(Sender: TObject); 
    procedure BitBtn4Click(Sender: TObject); 
    procedure FormClose(Sender: TObject; var Action: TCloseAction); 
    procedure FormCreate(Sender: TObject); 
    procedure FormKeyDown(Sender: TObject; var Key: Word; 
      Shift: TShiftState); 
    procedure DBGridEh1KeyDown(Sender: TObject; var Key: Word; 
      Shift: TShiftState); 
    procedure DBGridEh1MouseMove(Sender: TObject; Shift: TShiftState; X, 
      Y: Integer); 
    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); 
  private 
    NewFlag: Boolean; 
    EditFlag: Boolean; 
  public 
    { Public declarations } 
  end; 
 
var 
  EnterEmployeeForm: TEnterEmployeeForm; 
 
implementation 
 
uses Main; 
 
{$R *.dfm} 
 
procedure TEnterEmployeeForm.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.Text := Combobox1.EditValue; 
        Edit1.SetFocus; 
        Abort; 
      end; 
    end; 
  end; 
end; 
 
procedure TEnterEmployeeForm.Combobox1KeyPress(Sender: TObject; var Key: Char); 
begin 
  if (Key = #13) then 
  begin 
    Edit1.SetFocus; 
  end; 
end; 
 
procedure TEnterEmployeeForm.Edit1KeyPress(Sender: TObject; var Key: Char); 
begin 
  if (Key = #13) then 
  begin 
    Edit2.SetFocus; 
  end; 
end; 
 
procedure TEnterEmployeeForm.Edit2KeyPress(Sender: TObject; var Key: Char); 
begin 
  if (Key = #13) then 
  begin 
    BitBtn3.SetFocus; 
  end; 
end; 
 
procedure TEnterEmployeeForm.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; 
      Edit2.Clear; 
      Combobox1.SetFocus; 
    end 
    else 
    begin 
      Abort; 
    end; 
  end 
  else 
  begin 
    NewFlag := True; 
    GroupBox1.Enabled := True; 
    DBGridEh1.Enabled := True; 
    Edit1.Clear; 
    Edit2.Clear; 
    Combobox1.SetFocus; 
  end; 
end; 
 
procedure TEnterEmployeeForm.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.fieldbyname('员工姓名').AsString; 
        Edit2.Text := DataQuery.fieldbyname('计算机名称').AsString; 
        Combobox1.EditValue := DataQuery.fieldbyname('部门名称').AsString; 
        IPEdit1.IPString := DataQuery.fieldbyname('计算机地址').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.fieldbyname('员工姓名').AsString; 
        Edit2.Text := DataQuery.fieldbyname('计算机名称').AsString; 
        Combobox1.EditValue := DataQuery.fieldbyname('部门名称').AsString; 
        IPEdit1.IPString := DataQuery.fieldbyname('计算机地址').AsString; 
        Edit1.SetFocus; 
      end 
      else 
      begin 
        Abort; 
      end; 
    end; 
    EditFlag := True; 
    NewFlag := False; 
    GroupBox1.Enabled := True; 
    DBGridEh1.Enabled := False; 
    Edit1.Clear; 
    Edit1.Text := DataQuery.fieldbyname('员工姓名').AsString; 
    Edit2.Text := DataQuery.fieldbyname('计算机名称').AsString; 
    Combobox1.EditValue := DataQuery.fieldbyname('部门名称').AsString; 
    IPEdit1.IPString := DataQuery.fieldbyname('计算机地址').AsString; 
    Edit1.SetFocus; 
  end; 
end; 
 
procedure TEnterEmployeeForm.BitBtn3Click(Sender: TObject); 
begin 
  if (GroupBox1.Enabled = False) then 
  begin 
    ShowMessage('您并没有新建或修改过记录, 无需进行保存操作!'); 
    Abort; 
  end 
  else 
  begin 
    if (NewFlag = True) then 
    begin 
      if (Combobox1.Text = '') then 
      begin 
        ShowMessage('请选择所属产品型号,然后再进行保存,这是必填项!'); 
        Combobox1.SetFocus; 
        Abort; 
      end 
      else 
        if (Trim(Edit1.Text) = '') then 
      begin 
        ShowMessage('颜色为浅蓝色的输入栏是必填项, 请填入相应的信息然后再进行保存!'); 
        Edit1.SetFocus; 
        Abort; 
      end 
      else 
        if (Trim(Edit2.Text) = '') then 
      begin 
        ShowMessage('颜色为浅蓝色的输入栏是必填项, 请填入相应的信息然后再进行保存!'); 
        Edit2.SetFocus; 
        Abort; 
      end 
      else 
      begin 
        DataQuery.Active := True; 
        DataQuery.Append; 
        DataQuery.fieldbyname('员工姓名').AsString := Edit1.Text; 
        DataQuery.fieldbyname('计算机名称').AsString := Edit2.Text; 
        DataQuery.fieldbyname('部门名称').AsString := Combobox1.EditValue; 
        DataQuery.fieldbyname('计算机地址').AsString := IPEdit1.IPString; 
        DataQuery.Post; 
        DataQuery.Active := False; 
        DataQuery.Active := True; 
        Edit1.Clear; 
        Edit2.Clear; 
        GroupBox1.Enabled := False; 
        NewFlag := False; 
        BitBtn1.SetFocus; 
      end; 
    end; 
    //////////////////////////////////////////////////////////////////////// 
    //以下为修改保存语句 
    /////////////////////////////////////////////////////////////////////// 
 
    if (EditFlag = True) then 
    begin 
      if (Combobox1.Text = '') then 
      begin 
        ShowMessage('请选择所属部门,然后再进行保存,这是必填项!'); 
        Combobox1.SetFocus; 
        Abort; 
      end 
      else 
        if (Trim(Edit1.Text) = '') then 
      begin 
        ShowMessage('颜色为浅蓝色的输入栏是必填项, 请填入相应的信息然后再进行保存!'); 
        Edit1.SetFocus; 
        Abort; 
      end 
      else 
        if (Trim(Edit2.Text) = '') then 
      begin 
        ShowMessage('颜色为浅蓝色的输入栏是必填项, 请填入相应的信息然后再进行保存!'); 
        Edit2.SetFocus; 
        Abort; 
      end 
      else 
      begin 
        DataQuery.Active := True; 
        DataQuery.Edit; 
        DataQuery.fieldbyname('员工姓名').AsString := Edit1.Text; 
        DataQuery.fieldbyname('计算机名称').AsString := Edit2.Text; 
        DataQuery.fieldbyname('部门名称').AsString := Combobox1.EditValue; 
        DataQuery.fieldbyname('计算机地址').AsString := IPEdit1.IPString; 
        DataQuery.Post; 
        DataQuery.Active := False; 
        DataQuery.Active := True; 
        Edit1.Clear; 
        Edit2.Clear; 
        GroupBox1.Enabled := False; 
        EditFlag := False; 
        BitBtn1.SetFocus; 
        DBGridEh1.Enabled := True; 
      end; 
    end; 
  end; 
end; 
 
procedure TEnterEmployeeForm.BitBtn4Click(Sender: TObject); 
begin 
  Close; 
end; 
 
procedure TEnterEmployeeForm.FormClose(Sender: TObject; var Action: TCloseAction); 
begin 
  Action := CaFree; 
end; 
 
procedure TEnterEmployeeForm.FormCreate(Sender: TObject); 
begin 
  NewFlag := False; 
  EditFlag := False; 
end; 
 
procedure TEnterEmployeeForm.FormKeyDown(Sender: TObject; var Key: Word; 
  Shift: TShiftState); 
begin 
  if (Key = VK_Escape) then 
  begin 
    Edit1.Clear; 
    Edit2.Clear; 
    Combobox1.Enabled := True; 
    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 TEnterEmployeeForm.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 TEnterEmployeeForm.DBGridEh1MouseMove(Sender: TObject; 
  Shift: TShiftState; X, Y: Integer); 
begin 
  StatusBar1.Panels[1].Text := '选择要删除的记录,按DEL键删除选中的记录,按CTRL+F可以在表中查找'; 
end; 
 
procedure TEnterEmployeeForm.BitBtn1MouseMove(Sender: TObject; 
  Shift: TShiftState; X, Y: Integer); 
begin 
  StatusBar1.Panels[1].Text := '按下本按钮将会新建一条新记录'; 
end; 
 
procedure TEnterEmployeeForm.BitBtn2MouseMove(Sender: TObject; 
  Shift: TShiftState; X, Y: Integer); 
begin 
  StatusBar1.Panels[1].Text := '按下本按钮将会修改选中的一条记录'; 
end; 
 
procedure TEnterEmployeeForm.BitBtn3MouseMove(Sender: TObject; 
  Shift: TShiftState; X, Y: Integer); 
begin 
  StatusBar1.Panels[1].Text := '按下本按钮将会保存当前的记录'; 
end; 
 
procedure TEnterEmployeeForm.BitBtn4MouseMove(Sender: TObject; 
  Shift: TShiftState; X, Y: Integer); 
begin 
  StatusBar1.Panels[1].Text := '按下本按钮将会关闭本窗口'; 
end; 
 
end.