uprkind.pas
来自「DELPHI编程入门篇.从基础入手,浅显易懂,一定物有所值.」· PAS 代码 · 共 290 行
PAS
290 行
unit Uprkind;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, StdCtrls, Buttons, Grids;
type
Tfrmprkind = class(TForm)
StringGrid1: TStringGrid;
BitBtn1: TBitBtn;
BitBtn5: TBitBtn;
BitBtn2: TBitBtn;
BitBtn3: TBitBtn;
BitBtn4: TBitBtn;
ADOQuery1: TADOQuery;
BitBtn6: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn5Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure BitBtn6Click(Sender: TObject);
procedure BitBtn4Click(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
procedure Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure StringGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
procedure StringGrid1KeyPress(Sender: TObject; var Key: Char);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmprkind: Tfrmprkind;
implementation
uses Udatamodule;
{$R *.dfm}
procedure Tfrmprkind.BitBtn1Click(Sender: TObject);
begin
if (stringgrid1.Cells[1,stringgrid1.RowCount-1] <> '') and (stringgrid1.Cells[2,stringgrid1.RowCount-1] <> '') then
stringgrid1.RowCount:=stringgrid1.RowCount+1;
stringgrid1.Options:=stringgrid1.Options+[goEditing];
stringgrid1.Cells[1,stringgrid1.RowCount-1]:=inttostr(strtoint(stringgrid1.Cells[1,stringgrid1.rowcount-2])+1);
while length(stringgrid1.Cells[1,stringgrid1.RowCount-1]) < 5 do
stringgrid1.Cells[1,stringgrid1.RowCount-1]:='0'+stringgrid1.Cells[1,stringgrid1.RowCount-1];
stringgrid1.Row:=stringgrid1.RowCount-1;
stringgrid1.SetFocus;
bitbtn5.Enabled:=false;
bitbtn4.Enabled:=false;
bitbtn2.Enabled:=false;
bitbtn1.Enabled:=false;
bitbtn3.Enabled:=true;
bitbtn6.Enabled:=true;
end;
procedure Tfrmprkind.BitBtn5Click(Sender: TObject);
begin
stringgrid1.Options:=stringgrid1.Options+[goEditing];
bitbtn5.Enabled:=false;
bitbtn4.Enabled:=false;
bitbtn2.Enabled:=false;
bitbtn1.Enabled:=false;
bitbtn3.Enabled:=true;
bitbtn6.Enabled:=true;
end;
procedure Tfrmprkind.BitBtn2Click(Sender: TObject);
var
i:integer;
begin
if stringgrid1.Row<>0 then
begin
adoquery1.SQL.Text:='select classid from pluinfo where classid = '''+stringgrid1.Cells[1,stringgrid1.Row]+'''';
adoquery1.Open;
if adoquery1.RecordCount = 0 then
begin
if (messagedlg('真的要删除该类吗?',mtConfirmation ,[mbyes,mbno],0) = mryes) then
begin
i:=stringgrid1.Row;
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('delete from classinfo where classid = '''+stringgrid1.Cells[1,i]+'''');
adoquery1.ExecSQL;
adoquery1.Close;
if stringgrid1.RowCount > 2 then
begin
for i:=stringgrid1.Row to stringgrid1.RowCount-2 do
stringgrid1.Rows[i]:=stringgrid1.Rows[i+1];
stringgrid1.Rows[stringgrid1.RowCount-1].Clear;
stringgrid1.RowCount:=stringgrid1.RowCount-1;
end
else
stringgrid1.Rows[i].Clear;
end;
end
else
showmessage('类下面有产品,不能删除!');
end
else
showmessage('请选择要删除的项!');
end;
procedure Tfrmprkind.FormCreate(Sender: TObject);
var
arow:integer;
begin
stringgrid1.Cells[1,0]:='类编号';
stringgrid1.Cells[2,0]:='类名称';
adoquery1.SQL.Text:='select * from classinfo order by classid';
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.FieldValues['classid'];
stringgrid1.Cells[2,arow]:=adoquery1.FieldValues['classname'];
inc(arow);
adoquery1.Next;
end;
adoquery1.Close;
end;
end;
procedure Tfrmprkind.BitBtn6Click(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 * from classinfo';
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.FieldValues['classid'];
stringgrid1.Cells[2,arow]:=adoquery1.FieldValues['classname'];
inc(arow);
adoquery1.Next;
end;
adoquery1.Close;
end;
bitbtn5.Enabled:=true;
bitbtn4.Enabled:=true;
bitbtn2.Enabled:=true;
bitbtn1.Enabled:=true;
bitbtn3.Enabled:=false;
bitbtn6.Enabled:=false;
end;
procedure Tfrmprkind.BitBtn4Click(Sender: TObject);
begin
self.Close;
end;
procedure Tfrmprkind.BitBtn3Click(Sender: TObject);
var
i:integer;
begin
for i:=1 to stringgrid1.RowCount-1 do
begin
if stringgrid1.Cells[1,i] = '' then
begin
showmessage('第'+inttostr(i)+'行类编号不能为空!');
abort;
end;
if stringgrid1.Cells[2,i] = '' then
begin
showmessage('第'+inttostr(i)+'行类名称不能为空!');
abort;
end;
end;
for i:=1 to stringgrid1.RowCount-1 do
begin
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select classid,classname from classinfo where classid = '''+stringgrid1.Cells[1,i]+'''');
adoquery1.Open;
adoquery1.Edit;
adoquery1.Fields[0].AsString:=stringgrid1.Cells[1,i];
adoquery1.Fields[1].AsString:=stringgrid1.Cells[2,i];
adoquery1.Post;
adoquery1.Close;
end;
showmessage('保存成功!');
bitbtn5.Enabled:=true;
bitbtn4.Enabled:=true;
bitbtn2.Enabled:=true;
bitbtn1.Enabled:=true;
bitbtn3.Enabled:=false;
bitbtn6.Enabled:=false;
stringgrid1.Options:=stringgrid1.Options-[goediting] ;
end;
procedure Tfrmprkind.Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
showmessage(inttostr(key));
end;
procedure Tfrmprkind.StringGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if ((stringgrid1.Cells[2,stringgrid1.RowCount-1] <> '') and (key = 13) and (stringgrid1.Row=stringgrid1.RowCount-1)and (stringgrid1.Col = 2)) then
begin
stringgrid1.RowCount:=stringgrid1.RowCount+1;
stringgrid1.Cells[1,stringgrid1.RowCount-1]:=inttostr(strtoint(stringgrid1.Cells[1,stringgrid1.rowcount-2])+1);
while length(stringgrid1.Cells[1,stringgrid1.RowCount-1]) < 5 do
stringgrid1.Cells[1,stringgrid1.RowCount-1]:='0'+stringgrid1.Cells[1,stringgrid1.RowCount-1];
end;
//else
// stringgrid1.Row:=stringgrid1.RowCount-1;
end;
procedure Tfrmprkind.StringGrid1SelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
begin
if ((arow <> stringgrid1.RowCount-1) and (stringgrid1.Cells[2,stringgrid1.RowCount-1] = '' )) then
begin
stringgrid1.Rows[stringgrid1.RowCount-1].Clear;
stringgrid1.RowCount:=stringgrid1.RowCount-1;
end;
end;
procedure Tfrmprkind.StringGrid1KeyPress(Sender: TObject; var Key: Char);
var
i:integer;
s:string;//记录当前行的类编号
arow:integer;//当前行数
begin
if stringgrid1.Col = 1 then
if ((not (key in ['0'..'9'])) and (key <> #8) and (key <> #22)) then
begin
showmessage('输入数字');
abort;
end;
if stringgrid1.Col = 2 then
begin
arow:=stringgrid1.Row;
s:=stringgrid1.Cells[1,arow];
for i:=1 to stringgrid1.RowCount-1 do
begin
if i<>arow then
begin
if s= stringgrid1.Cells[1,i] then
begin
showmessage('类编号有重复,请检查输入是否正确!');
stringgrid1.Cells[1,arow]:='';
abort;
end;
end;
if length(stringgrid1.Cells[1,i]) > 5 then
begin
showmessage('类编号不大于5位!');
abort;
end
else
while length(stringgrid1.Cells[1,i]) < 5 do
stringgrid1.Cells[1,i]:='0'+stringgrid1.Cells[1,i];
end;
end;
end;
procedure Tfrmprkind.FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
begin
if not bitbtn4.Enabled then
begin
canclose:=false;
showmessage('请先保存数据然后退出');
end;
end;
end.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?