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

📄 ubusary.pas

📁 DELPHI编程入门篇.从基础入手,浅显易懂,一定物有所值.
💻 PAS
字号:
unit Ubusary;

interface

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

type
  Tfrmbusary = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    StringGrid1: TStringGrid;
    Panel4: TPanel;
    StringGrid2: TStringGrid;
    Label1: TLabel;
    ComboBox1: TComboBox;
    Label2: TLabel;
    ComboBox2: TComboBox;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    ADOQuery1: TADOQuery;
    Button6: TButton;
    Button7: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure StringGrid2DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure StringGrid2SelectCell(Sender: TObject; ACol, ARow: Integer;
      var CanSelect: Boolean);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure ComboBox1Select(Sender: TObject);
    procedure ComboBox2Select(Sender: TObject);
    procedure StringGrid2KeyPress(Sender: TObject; var Key: Char);
    procedure StringGrid1DblClick(Sender: TObject);
    procedure Button7Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmbusary: Tfrmbusary;

implementation

uses Udatamodule;

{$R *.dfm}

procedure Tfrmbusary.FormCreate(Sender: TObject);
var
  i,arow:integer;
begin
  stringgrid1.Cells[1,0]:='商品编号';
  stringgrid1.Cells[2,0]:='商品种类';
  stringgrid1.Cells[3,0]:='商品名称';
  stringgrid2.Cells[1,0]:='商品编号';
  stringgrid2.Cells[2,0]:='商品名称';
  stringgrid2.Cells[3,0]:='单位';
  stringgrid2.Cells[4,0]:='数量';
  stringgrid2.Cells[5,0]:='库存下限';
  stringgrid2.Cells[6,0]:='最优库存';
  adoquery1.SQL.Text:='select classid,classname from classinfo order by classid';
  adoquery1.Open;
  while not adoquery1.Eof do
  begin
    combobox1.Items.Add(adoquery1.fields[0].asstring+'   '+adoquery1.Fields[1].asstring);
    adoquery1.Next;
  end;
  adoquery1.Close;
  adoquery1.SQL.Text:='select prid,classid,prname from pluinfo';
  adoquery1.Open;
  if adoquery1.RecordCount > 0 then
  begin
    i:=1;
    stringgrid1.RowCount:=adoquery1.RecordCount+1;
    while not adoquery1.Eof do
    begin
      stringgrid1.Cells[1,i]:=adoquery1.Fields[0].AsString;
      stringgrid1.Cells[2,i]:=adoquery1.Fields[1].AsString;
      stringgrid1.Cells[3,i]:=adoquery1.Fields[2].AsString;
      adoquery1.Next;
      inc(i);
    end;
  end;
  adoquery1.Close;
  adoquery1.SQL.Text:='select filid,filname from filiale';
  adoquery1.Open;
  while not adoquery1.Eof do
  begin
    combobox2.Items.Add(adoquery1.Fields[0].asstring+'  |  '+adoquery1.Fields[1].asstring);
    adoquery1.Next;
  end;  
  adoquery1.Close;
  combobox2.ItemIndex:=0;
  if combobox2.Text <> '' then
  begin
    for arow:=1 to stringgrid2.RowCount-1 do
    stringgrid2.Rows[arow].Clear;
    stringgrid2.RowCount:=2;
    adoquery1.SQL.Text:='select busary.prid,pluinfo.prname,busary.unitage,'+
    'busary.busanum,busary.stocklow,busary.stockhigh from busary inner join'+
    ' pluinfo on busary.prid = pluinfo.prid where filid = '''+copy(combobox2.Text,1,4)+'''';
    adoquery1.Open;
    if adoquery1.RecordCount > 0 then
    begin
      stringgrid2.RowCount:=adoquery1.RecordCount+1;
      arow:=1;
      while not adoquery1.Eof do
      begin
        for i:=1 to stringgrid2.ColCount-1 do
          stringgrid2.Cells[i,arow]:=adoquery1.Fields[i-1].AsString;
        adoquery1.Next;
        inc(arow);
      end;
      stringgrid2.Row:=1;
      stringgrid2.Cells[0,1]:='*';
    end;
    adoquery1.Close;
  end;  
end;

procedure Tfrmbusary.Button1Click(Sender: TObject);
var
  i,pcount:integer;
begin
  if stringgrid1.Row > 0 then
  begin
    pcount:=0;
    for i:=1 to stringgrid2.RowCount-1 do
      if stringgrid2.Cells[1,i] = stringgrid1.Cells[1,stringgrid1.Row] then
      pcount:=pcount+1;
    if pcount = 0 then
    begin
      if stringgrid2.Cells[1,stringgrid2.RowCount-1] <> '' then
        stringgrid2.RowCount:=stringgrid2.RowCount + 1;
      stringgrid2.Cells[1,stringgrid2.RowCount-1]:=stringgrid1.Cells[1,stringgrid1.Row];
      stringgrid2.Cells[2,stringgrid2.RowCount-1]:=stringgrid1.Cells[3,stringgrid1.Row];
    end;
    button6.Enabled:=true;
  end;  
end;

procedure Tfrmbusary.StringGrid2DrawCell(Sender: TObject; ACol,
  ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
  if ((arow > 0) and (acol > 2)) then
  begin
    stringgrid2.Canvas.Brush.Color:=clwindow;
    stringgrid2.Canvas.FillRect(rect);
    stringgrid2.Canvas.font.Color:=clblack;
    stringgrid2.Canvas.TextOut(Rect.Left+2,Rect.Top+2,StringGrid2.Cells[aCol,aRow ]);
  end;
end;

procedure Tfrmbusary.StringGrid2SelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
begin
  stringgrid2.Cols[0].Clear;
  stringgrid2.Cells[0,arow]:='*';
  if acol > 2 then
    StringGrid2.Options:=stringgrid2.Options+[goediting]
  else
    stringgrid2.Options:=stringgrid2.Options-[goediting];
end;

procedure Tfrmbusary.Button2Click(Sender: TObject);
var
  i:integer;
begin
  if stringgrid2.Row > 0 then
  begin
    if stringgrid2.RowCount > 2 then
    begin
      for i:=stringgrid2.Row to stringgrid1.RowCount - 1 do
        stringgrid2.Rows[i]:=stringgrid2.Rows[i+1];
      stringgrid2.rows[stringgrid1.RowCount-1].Clear;
      stringgrid2.RowCount:=stringgrid2.RowCount-1;
    end
    else
      stringgrid2.Rows[1].Clear;
  end;    
end;

procedure Tfrmbusary.Button3Click(Sender: TObject);
var
  i,j:integer;
  pcount:integer;
begin
  if stringgrid1.Cells[1,1] <> '' then
  begin
    for i:=1 to stringgrid1.RowCount -1 do
    begin
      pcount:=0;
      for j:=1 to stringgrid2.RowCount-1 do
        if stringgrid2.Cells[1,j] = stringgrid1.Cells[1,i] then
          pcount:=pcount+1;
      if pcount = 0 then
      begin
        if stringgrid2.Cells[1,stringgrid2.RowCount-1] <> '' then
        stringgrid2.RowCount:=stringgrid2.RowCount + 1;
        stringgrid2.Cells[1,stringgrid2.RowCount-1]:=stringgrid1.Cells[1,i];
        stringgrid2.Cells[2,stringgrid2.RowCount-1]:=stringgrid1.Cells[3,i];
      end;
    end;
    stringgrid2.Cells[0,1]:='*';
    stringgrid2.Row:=1;
    button6.Enabled:=true;
  end;        
end;

procedure Tfrmbusary.Button4Click(Sender: TObject);
var
  i:integer;
begin
  if stringgrid2.Cells[1,1] <> '' then
  begin
    if messagedlg('确定要全部删除吗?',mtconfirmation,[mbyes,mbno],0) = mryes then
    begin
      for i:=1 to stringgrid2.RowCount-1 do
        stringgrid2.Rows[i].Clear;
      stringgrid2.RowCount:=2;
    end;
  end;
end;

procedure Tfrmbusary.Button5Click(Sender: TObject);
begin
  self.Close;
end;

procedure Tfrmbusary.Button6Click(Sender: TObject);
var
  i:integer;
begin
  for i:=1 to stringgrid2.RowCount-1 do
  begin
    if stringgrid2.Cells[1,i] = '' then
    begin
      showmessage('第'+inttostr(i)+'行商品编号不能为空');
      exit;
    end;
    //if stringgrid2.Cells[3,i] = '' then
    //begin
     // showmessage('第'+inttostr(i)+'行单位不能为空');
    //  exit;
   // end;
    if (stringgrid2.Cells[4,i] <> '') and (stringgrid2.Cells[5,i] <> '') then
    begin
      if strtofloat(stringgrid2.Cells[4,i]) >  strtofloat(stringgrid2.Cells[5,i]) then
      begin
        showmessage('第'+inttostr(i)+'行库存下限不能大于最优库存');
        exit;
      end;
    end;
    if stringgrid2.Cells[3,i] = '' then
    begin
      showmessage('第'+inttostr(i)+'行库存数量不能为空');
      exit;
    end;
  end;
  if stringgrid2.Cells[1,1] <> '' then
  begin
    for i:=1 to stringgrid2.RowCount -1 do
    begin
      adoquery1.SQL.Text:='select filid,prid,unitage,busanum,stocklow,stockhigh'+
      ' from busary where prid = '''+stringgrid2.Cells[1,i]+''' and filid = '''+copy(combobox2.Text,1,4)+'''';
      adoquery1.Open;
      adoquery1.Edit;
      adoquery1.FieldValues['filid']:=copy(combobox2.Text,1,4);
      adoquery1.FieldValues['prid']:=stringgrid2.Cells[1,i];
      adoquery1.FieldValues['unitage']:=stringgrid2.Cells[3,i];
      adoquery1.fieldbyname('busanum').AsString:=stringgrid2.Cells[4,i];
      adoquery1.fieldbyname('stocklow').AsString:=stringgrid2.Cells[5,i];
      adoquery1.fieldbyname('stockhigh').AsString:=stringgrid2.Cells[6,i];
      adoquery1.Post;
    end;
    adoquery1.Close;
    showmessage('保存成功!');
    button6.Enabled:=false; 
  end
  else
    showmessage('请先添加商品信息');
end;

procedure Tfrmbusary.ComboBox1Select(Sender: TObject);
var
  arow:integer;
begin
  for arow:=1 to stringgrid1.RowCount-1 do
  stringgrid1.Rows[arow].Clear;
  stringgrid1.RowCount:=2;
  adoquery1.SQL.Text:='select prid,classid,prname from pluinfo';
  if combobox1.Text <> '全部' then
    adoquery1.SQL.Add('where classid = '''+copy(combobox1.Text,1,5)+'''');
  adoquery1.Open;
  if adoquery1.RecordCount > 0 then
  begin
    stringgrid1.RowCount:=adoquery1.RecordCount+1;
    arow:=1;
    while not adoquery1.Eof do
    begin
      stringgrid1.Cells[1,arow]:=adoquery1.Fields[0].AsString;
      stringgrid1.Cells[2,arow]:=adoquery1.Fields[1].AsString;
      stringgrid1.Cells[3,arow]:=adoquery1.Fields[2].AsString;
      adoquery1.Next;
      inc(arow);
    end;
  end;
end;

procedure Tfrmbusary.ComboBox2Select(Sender: TObject);
var
  i,arow:integer;
begin
  if combobox2.Text <> '' then
  begin
    for arow:=1 to stringgrid2.RowCount-1 do
    stringgrid2.Rows[arow].Clear;
    stringgrid2.RowCount:=2;
    adoquery1.SQL.Text:='select busary.prid,pluinfo.prname,busary.unitage,'+
    'busary.busanum,busary.stocklow,busary.stockhigh from busary inner join'+
    ' pluinfo on busary.prid = pluinfo.prid where filid = '''+copy(combobox2.Text,1,4)+'''';
    adoquery1.Open;
    if adoquery1.RecordCount > 0 then
    begin
      stringgrid2.RowCount:=adoquery1.RecordCount+1;
      arow:=1;
      while not adoquery1.Eof do
      begin
        for i:=1 to stringgrid2.ColCount-1 do
          stringgrid2.Cells[i,arow]:=adoquery1.Fields[i-1].AsString;
        adoquery1.Next;
        inc(arow);
      end;
      stringgrid2.Row:=1;
      stringgrid2.Cells[0,1]:='*';
    end;
    adoquery1.Close;
  end;  
end;

procedure Tfrmbusary.StringGrid2KeyPress(Sender: TObject; var Key: Char);
var
  acol:integer;
  s:string;
  i,cpoint:integer;
begin
  acol:=stringgrid2.Col;
  if aCol = 3 then
  begin
    if length(stringgrid2.Cells[aCol,stringgrid2.Row]) >=10 then
      stringgrid2.Cells[aCol,stringgrid2.Row]:=copy(stringgrid2.Cells[aCol,stringgrid2.Row],1,10);
  end;
  if ((acol = 4) or (acol = 5) or (acol = 6)) then
  begin
    if (( not(key in ['0'..'9'])) and (key <> '.') and (key <> #8) and (key <> #22)) then
    begin
      showmessage('输入数字或小数点');
      key:=chr(0);
      exit;
    end
    else begin
      s:=stringgrid2.Cells[acol,stringgrid2.Row];
      cpoint:=0;
      for i:=1 to length(s) do
        if s[i] = '.' then
        inc(cpoint);
      if ((cpoint > 0) and (key = '.')) then
      key:=chr(0);
    end;
  end;
end;

procedure Tfrmbusary.StringGrid1DblClick(Sender: TObject);
var
  i,pcount:integer;
begin
  if ((stringgrid1.Row > 0) and (stringgrid1.Cells[1,1] <> '')) then
  begin
    pcount:=0;
    for i:=1 to stringgrid2.RowCount-1 do
      if stringgrid2.Cells[1,i] = stringgrid1.Cells[1,stringgrid1.Row] then
      begin
        pcount:=pcount+1;
        break;
      end;
    if pcount = 0 then
    begin
      if stringgrid2.Cells[1,stringgrid2.RowCount-1] <> '' then
        stringgrid2.RowCount:=stringgrid2.RowCount + 1;
      stringgrid2.Cells[1,stringgrid2.RowCount-1]:=stringgrid1.Cells[1,stringgrid1.Row];
      stringgrid2.Cells[2,stringgrid2.RowCount-1]:=stringgrid1.Cells[3,stringgrid1.Row];
      button6.Enabled:=true;
    end;
  end;  
end;

procedure Tfrmbusary.Button7Click(Sender: TObject);
begin
  button6.Enabled:=true;
end;

end.

⌨️ 快捷键说明

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