ufiliale.pas

来自「DELPHI编程入门篇.从基础入手,浅显易懂,一定物有所值.」· PAS 代码 · 共 285 行

PAS
285
字号
unit Ufiliale;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ExtCtrls, Grids, DB, ADODB;

type
  Tfrmfiliale = class(TForm)
    Label1: TLabel;
    StringGrid1: TStringGrid;
    Panel1: TPanel;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    BitBtn3: TBitBtn;
    BitBtn4: TBitBtn;
    BitBtn5: TBitBtn;
    BitBtn6: TBitBtn;
    Label2: TLabel;
    Edit1: TEdit;
    Panel2: TPanel;
    Label3: TLabel;
    Edit2: TEdit;
    Label4: TLabel;
    Edit3: TEdit;
    Label5: TLabel;
    Edit4: TEdit;
    Label6: TLabel;
    Edit5: TEdit;
    filialeQuery: TADOQuery;
    procedure FormCreate(Sender: TObject);
    procedure BitBtn4Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn5Click(Sender: TObject);
    procedure StringGrid1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
    procedure BitBtn6Click(Sender: TObject);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmfiliale: Tfrmfiliale;

implementation

uses Udatamodule;

{$R *.dfm}

procedure Tfrmfiliale.FormCreate(Sender: TObject);
var
  i:integer;
  arow:integer;//表格当前行
begin
  stringgrid1.Cells[0,0]:='分店编号';
  stringgrid1.Cells[1,0]:='分店名称';
  stringgrid1.Cells[2,0]:='联系电话';
  stringgrid1.Cells[3,0]:='联系人';
  stringgrid1.Cells[4,0]:='分店地址';
  for i:=1 to stringgrid1.RowCount-1 do
  stringgrid1.Rows[i].Clear;
  stringgrid1.RowCount:=2;
  filialeQuery.SQL.Text:='select * from filiale order by filid';
  filialeQuery.Open;
  if filialequery.RecordCount > 0 then
  begin
    arow:=1;
    stringgrid1.RowCount:=filialequery.RecordCount+1;
    while not filialequery.Eof do
    begin
      stringgrid1.Cells[0,arow]:=filialequery.FieldValues['filid'];
      stringgrid1.Cells[1,arow]:=filialequery.FieldValues['filname'];
      stringgrid1.Cells[2,arow]:=filialequery.FieldValues['filtel'];
      stringgrid1.Cells[3,arow]:=filialequery.FieldValues['functionary'];
      stringgrid1.Cells[4,arow]:=filialequery.FieldValues['address'];
      filialequery.Next;
      inc(arow);
    end;
    filialequery.Close;
    edit1.Text:=stringgrid1.Cells[0,1];
    edit2.Text:=stringgrid1.Cells[1,1];
    edit3.Text:=stringgrid1.Cells[2,1];
    edit4.Text:=stringgrid1.Cells[3,1];
    edit5.Text:=stringgrid1.Cells[4,1];
  end;
end;

procedure Tfrmfiliale.BitBtn4Click(Sender: TObject); //添加
var
  sstr:string;
begin
  bitbtn1.Enabled:=true;
  bitbtn5.Enabled:=true;
  bitbtn2.Enabled:=false;
  bitbtn3.Enabled:=false;
  bitbtn4.Enabled:=false;
  bitbtn6.Enabled:=false;
  edit2.Enabled:=true; edit2.Text:='';
  edit3.Enabled:=true; edit3.Text:='';
  edit4.Enabled:=true; edit4.Text:='';
  edit5.Enabled:=true; edit5.Text:='';
  filialequery.Close;
  filialequery.SQL.Text:='select max(filid) as counts from filiale';
  filialequery.Open;
  sstr:=filialequery.fieldbyname('counts').AsString;
  if sstr = '' then
    sstr:='0001'
  else
  begin
    sstr:=inttostr(strtoint(sstr)+1);
    while length(sstr) < 4 do
      sstr:='0'+sstr;
  end;
  edit1.Text:=sstr;
  edit2.SetFocus;
end;

procedure Tfrmfiliale.BitBtn1Click(Sender: TObject);//保存
var
  arow:integer;//表格行
begin
  if edit1.Text = '' then
  begin
    showmessage('分店编号不能为空');
    edit1.SetFocus;
    exit;
  end;
  if edit2.Text = '' then
  begin
    showmessage('请输入名称');
    edit2.SetFocus;
    exit;
  end;
  if edit4.Text = '' then
  begin
    showmessage('请输入联系人');
    edit3.SetFocus;
    exit;
  end;
  filialequery.SQL.Text:='select filid from filiale where filid = '''+edit1.Text+'''';
  filialequery.Open;
  if filialequery.RecordCount > 0 then
  begin
    filialequery.Close;
    filialequery.SQL.Text:='update filiale set filname = '''+edit2.Text+''', filtel = '''+edit3.Text+''', functionary = '''+edit4.Text+''', address = '''+edit5.Text+''' where filid = '''+edit1.Text+'''';
    filialequery.ExecSQL;
    filialequery.Close;
    for arow:=1 to stringgrid1.RowCount-1 do
      if stringgrid1.Cells[0,arow] = edit1.Text then
      begin
        stringgrid1.Cells[1,arow]:=edit2.Text;
        stringgrid1.Cells[2,arow]:=edit3.Text;
        stringgrid1.Cells[3,arow]:=edit4.Text;
        stringgrid1.Cells[4,arow]:=edit5.Text;
      end;
  end
  else
  begin
    filialequery.Close;
    filialequery.SQL.Text:='insert into filiale values("'+edit1.Text+'","'+edit2.Text+'","'+edit3.Text+'","'+edit4.Text+'","'+edit5.Text+'")';
    filialequery.ExecSQL;
    filialequery.Close;
    if stringgrid1.Cells[0,stringgrid1.RowCount-1] <> '' then
      stringgrid1.RowCount:=stringgrid1.RowCount+1;
    arow:=stringgrid1.RowCount-1;
    stringgrid1.Cells[0,arow]:=edit1.Text;
    stringgrid1.Cells[1,arow]:=edit2.Text;
    stringgrid1.Cells[2,arow]:=edit3.Text;
    stringgrid1.Cells[3,arow]:=edit4.Text;
    stringgrid1.Cells[4,arow]:=edit5.Text;
  end;
  bitbtn1.Enabled:=false;
  bitbtn5.Enabled:=false;
  bitbtn2.Enabled:=true;
  bitbtn3.Enabled:=true;
  bitbtn4.Enabled:=true;
  bitbtn6.Enabled:=true;
  edit2.Enabled:=false;
  edit3.Enabled:=false;
  edit4.Enabled:=false;
  edit5.Enabled:=false; 
end;

procedure Tfrmfiliale.BitBtn5Click(Sender: TObject); //取消
begin
  bitbtn1.Enabled:=false;
  bitbtn5.Enabled:=false;
  bitbtn2.Enabled:=true;
  bitbtn3.Enabled:=true;
  bitbtn4.Enabled:=true;
  bitbtn6.Enabled:=true;
  edit2.Enabled:=false;
  edit3.Enabled:=false;
  edit4.Enabled:=false;
  edit5.Enabled:=false;
  stringgrid1.Row:=1;
  edit1.Text:=stringgrid1.Cells[0,1];
  edit2.Text:=stringgrid1.Cells[1,1];
  edit3.Text:=stringgrid1.Cells[2,1];
  edit4.Text:=stringgrid1.Cells[3,1];
  edit5.Text:=stringgrid1.Cells[4,1];
end;

procedure Tfrmfiliale.StringGrid1Click(Sender: TObject);
begin
  edit1.Text:=stringgrid1.Cells[0,stringgrid1.Row];
  edit2.Text:=stringgrid1.Cells[1,stringgrid1.Row];
  edit3.Text:=stringgrid1.Cells[2,stringgrid1.Row];
  edit4.Text:=stringgrid1.Cells[3,stringgrid1.Row];
  edit5.Text:=stringgrid1.Cells[4,stringgrid1.Row];
end;

procedure Tfrmfiliale.BitBtn2Click(Sender: TObject);
begin
  bitbtn1.Enabled:=true;
  bitbtn5.Enabled:=true;
  bitbtn2.Enabled:=false;
  bitbtn3.Enabled:=false;
  bitbtn4.Enabled:=false;
  bitbtn6.Enabled:=false;
  edit2.Enabled:=true;
  edit3.Enabled:=true;
  edit4.Enabled:=true;
  edit5.Enabled:=true;
  edit2.SetFocus;
end;

procedure Tfrmfiliale.BitBtn3Click(Sender: TObject);
var
  i:integer;
begin
  if ((stringgrid1.Row < stringgrid1.RowCount) and (stringgrid1.Row > 0)) then
  begin
    if messagedlg('真的要删除吗?',mtConfirmation,[mbYes, mbNo],0) = mrYes then
    begin
      filialequery.SQL.Text:='delete from filiale where filid = '''+stringgrid1.Cells[0,stringgrid1.row]+'''';
      filialequery.ExecSQL;
      filialequery.Close;
      for i:=stringgrid1.Row to stringgrid1.RowCount-2 do
        stringgrid1.Rows[i]:=stringgrid1.Rows[i+1];
      stringgrid1.Rows[stringgrid1.RowCount-1].Clear;
      if stringgrid1.RowCount > 2 then
      stringgrid1.RowCount:=stringgrid1.RowCount-1;
      stringgrid1.Row:=1;
      edit1.Text:=stringgrid1.Cells[0,1];
      edit2.Text:=stringgrid1.Cells[1,1];
      edit3.Text:=stringgrid1.Cells[2,1];
      edit4.Text:=stringgrid1.Cells[3,1];
      edit5.Text:=stringgrid1.Cells[4,1];
    end;
  end
  else
    showmessage('请选择要删除的项!');
end;

procedure Tfrmfiliale.BitBtn6Click(Sender: TObject);
begin
  self.Close;
end;

procedure Tfrmfiliale.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if (( not(key in ['0'..'9'])) and (key <> #8) and (key <> #22)) then
  begin
    showmessage('输入数字');
    key:=chr(0);
  end
end;

procedure Tfrmfiliale.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
  if not bitbtn6.Enabled then
  canclose:=false;
end;

end.

⌨️ 快捷键说明

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