我能写出以下这种代码,你看行不行。
这是一个用树形结构与数据库与DBGRID结合的,无限制插入货物的一张表单。其中一个类。
unit goods;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, ComCtrls, Grids, DBGrids, ImgList, Menus, DB, ADODB,
StdCtrls, DBCtrls;
type
Tfrmgoods = class(TForm)
Panel1: TPanel;
Splitter1: TSplitter;
Panel2: TPanel;
Splitter2: TSplitter;
Panel3: TPanel;
DBGrid1: TDBGrid;
TreeView1: TTreeView;
PopupMenu1: TPopupMenu;
PopupMenu2: TPopupMenu;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
N4: TMenuItem;
N5: TMenuItem;
N6: TMenuItem;
ImageList1: TImageList;
ImageList2: TImageList;
ADOQuery1: TADOQuery;
ADOQuery2: TADOQuery;
DataSource1: TDataSource;
Button1: TButton;
Button2: TButton;
Button3: TButton;
ADOTable1: TADOTable;
Button4: TButton;
DBComboBox1: TDBComboBox;
ADOQuery3: TADOQuery;
ADOQuery2g_num: TStringField;
ADOQuery2g_name: TStringField;
ADOQuery2g_dw: TStringField;
ADOQuery2g_Snum: TStringField;
ADOQuery2g_dz: TFloatField;
Button5: TButton;
Button6: TButton;
Button7: TButton;
Button8: TButton;
procedure FormActivate(Sender: TObject);
procedure TreeView1DblClick(Sender: TObject);
procedure N1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure N2Click(Sender: TObject);
procedure N3Click(Sender: TObject);
procedure N4Click(Sender: TObject);
procedure N5Click(Sender: TObject);
procedure N6Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure DBGrid1DrawColumnCell(Sender: TObject;
const Rect: TRect;
DataCol: Integer;
Column: TColumn;
State: TGridDrawState);
procedure DBGrid1KeyPress(Sender: TObject;
var Key: Char);
procedure DBGrid1ColExit(Sender: TObject);
procedure FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure Button8Click(Sender: TObject);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
goodsnum:string;
goodsname:string;
//goodstxm:string;
goodsdw:string;
goodssnum:string;
goodsvip:real;
function checkcf(str:string):boolean;
function adoupdatespending(adoquery:tcustomadodataset):boolean;
{ Public declarations }
end;
var
frmgoods: Tfrmgoods;
implementation
uses DataM,ATYPE,inputgoods,hzjpublic;
var typename:string;//用来得到输入的字符串
{$R *.dfm}
procedure Tfrmgoods.FormActivate(Sender: TObject);
var
thisnode:ttreenode;
string1:string;
a:boolean;
begin
a:=true;
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select type from h_forgoods where pcode=0');
adoquery1.Open;
adoquery1.First;
thisnode:=treeview1.Items.GetFirstNode;
thisnode.DeleteChildren;
while not (adoquery1.Eof)do
begin
if a then
begin
string1:=trim(adoquery1.FieldByName('type').AsString);
thisnode:=treeview1.Items.Addchild(thisnode,string1);
a:=false;
end
else
begin
string1:=trim(adoquery1.FieldByName('type').AsString);
thisnode:=treeview1.Items.Add(thisnode,string1);
end;
adoquery1.Next;
end;
adoquery2.Close;
adoquery2.SQL.Clear;
adoquery2.SQL.Add('select * from h_goods where g_code=0');
adoquery2.Open;
{query2.First;
while not (query2.Eof)do
begin
string1:=query2.FieldByName('姓名').AsString;
treeview1.Items.AddChild(thisnode,string1);
query2.Next;
end;
end;
}
treeview1.FullExpand;
treeview1.Selected:=treeview1.Items.GetFirstNode;
end;
procedure Tfrmgoods.TreeView1DblClick(Sender: TObject);
var
thisnode:ttreenode;
str,str1:string;
begin
thisnode:=treeview1.Selected;
thisnode.DeleteChildren;
str:=trim(treeview1.Selected.Text);
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select top 1 code from h_forgoods where rtrim(ltrim(type))='''+str+'''');
adoquery1.Open;
str:=trim(adoquery1.FieldByName('code').AsString);
str1:=str;
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select type from h_forgoods where rtrim(ltrim(pcode))='''+trim(str)+'''');
adoquery1.Open;
adoquery1.First;
while not (adoquery1.Eof)do
begin
//str:=adoquery1.FieldValues['type'];
str:=trim(adoquery1.FieldByName('type').AsString);
treeview1.Items.AddChild(thisnode,str);
adoquery1.Next;
end;
adoquery2.Close;
adoquery2.SQL.Clear;
adoquery2.SQL.Add('select * from h_goods where g_code='''+str1+'''');
adoquery2.Open;
end;
procedure Tfrmgoods.N1Click(Sender: TObject);
var
str:string;
thisnode:ttreenode;
code,max:integer;
code1:string;
frmcaption:string;
begin
frmcaption:='增加类别';
thisnode:=treeview1.Selected;
str:=trim(treeview1.Selected.Text);
if not checkcf(frmcaption) then
begin
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select top 1 code from h_forgoods where type='''+str+'''');
adoquery1.Open;
//code:=adoquery1.FieldValues['code'];
code:=adoquery1.FieldByName('code').AsInteger;
code1:=inttostr(code);
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select max(code) as max from h_forgoods');
adoquery1.Open;
//max:=adoquery1.FieldValues['max'];
max:=adoquery1.FieldByName('max').AsInteger;
max:=max+1;
try
adotable1.Connection.begin
Trans;
adotable1.Active:=true;
adotable1.Insert;
adotable1.FieldByName('pcode').AsInteger:=code;
adotable1.FieldByName('code').AsInteger:=max;
adotable1.FieldByName('type').AsString:=typename;
adotable1.FieldByName('child').AsInteger:=0;
adotable1.Post;
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('update h_forgoods set child=1 where code='''+code1+'''');
adoquery1.ExecSQL;
adotable1.Connection.CommitTrans;
treeview1.Items.AddChild(thisnode,typename)
except
on e:exception do
begin
showmessage(e.Message);
adotable1.Connection.RollbackTrans;
end;
end;
end;
end;
{if not (str='hzj') then
begin
application.MessageBox('输入的密码有错,请重输!','清空',mb_ok);
end
else
begin
if application.MessageBox('您是否想清空得分?','清空得分',mb_yesno+mb_defbutton2+mb_iconquestion)=idyes then
begin
end;
end;
}
function tfrmgoods.checkcf(str:string):boolean;
begin
checkcf:=false;
frmatype.Caption:=str;
frmatype.ShowModal;
if frmatype.atypebo then
begin
checkcf:=true;
exit;
end
else
begin
typename:=frmatype.atypestr;
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select * from h_forgoods where rtrim(ltrim(type))='''+trim(typename)+'''');
adoquery1.open;
if adoquery1.RecordCount>0 then
begin
showmessage('您输入重复的类别!请您再输一次!');
checkcf:=true;
end;
end;
end;
procedure Tfrmgoods.Button3Click(Sender: TObject);
begin
close;
end;
procedure Tfrmgoods.N2Click(Sender: TObject);
var
thisnode:ttreenode;
str,code:string;
child:integer;
rcount:integer;// 该类中是否存在着记录
begin
thisnode:=treeview1.Selected;
str:=trim(treeview1.Selected.Text);
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select * from h_forgoods where rtrim(ltrim(type))='''+trim(str)+'''');
adoquery1.Open;
//child:=adoquery1.FieldValues['child'];
child:=adoquery1.FieldByName('child').AsInteger;
//code:=adoquery1.FieldValues['code'];
code:=trim(adoquery1.FieldByName('code').AsString);
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select * from h_goods where rtrim(ltrim(g_code))='''+trim(code)+'''');
adoquery1.Open;
rcount:=adoquery1.RecordCount;
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select * from h_forgoods where rtrim(ltrim(pcode))='''+trim(code)+'''');
adoquery1.Open;
if adoquery1.RecordCount<1 then
begin
try
adoquery1.Connection.begin
Trans;
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('update h_forgoods set child=0 where rtrim(ltrim(code))='''+trim(code)+'''');
adoquery1.ExecSQL;
adoquery1.Connection.CommitTrans;
if strtoint(code)<>0 then
begin
child:=0;
end
else
begin
child:=1;
end;
except
adoquery1.Connection.RollbackTrans;
end;
end;
if (rcount>0) or (child=1) then
begin
showmessage('该类有子类别或存在商品记录,请先删除它们!');
end
else
begin
if application.MessageBox('您是否想删除该记录?','删除记录',mb_yesno+MB_ICONQUESTION+MB_DEFBUTTON2)=idyes then
begin
try
adoquery1.Connection.begin
Trans;
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('delete from h_forgoods where rtrim(ltrim(code))='''+trim(code)+'''');
adoquery1.ExecSQL;
adoquery1.Connection.CommitTrans;
treeview1.Selected.Delete;
except
on e:exceptiondo
begin
showmessage(e.Message);
adoquery1.Connection.RollbackTrans;
end;
end;
end;
end;
end;
procedure Tfrmgoods.N3Click(Sender: TObject);
var
thisnode:ttreenode;
str:string;
frmcaption:string;
begin
frmcaption:='修改类别';
thisnode:=treeview1.Selected;
str:=trim(treeview1.Selected.Text);
if str<>'所有商品' then
begin
if not checkcf(frmcaption) then
begin
try
adoquery1.Connection.begin
Trans;
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('update h_forgoods set type='''+trim(typename)+'''where rtrim(ltrim(type))='''+trim(str)+'''');
adoquery1.ExecSQL;
adoquery1.Connection.CommitTrans;
treeview1.Selected.Text:=trim(typename);
except
on e:exceptiondo
begin
showmessage(e.Message);
adoquery1.Connection.RollbackTrans;
end;
end;
end;
end;
end;
procedure Tfrmgoods.N4Click(Sender: TObject);
var
thisnode:ttreenode;
str:string;
code1:string;
strnum,strname,strdw,strsupply:string;
strvip:real;
begin
frminputgoods.ShowModal;
if frminputgoods.inputgoodsbo then
begin
strnum:=frminputgoods.inputgoodsnum;
strname:=frminputgoods.inputgoodsname;
strdw:=frminputgoods.inputgoodsdw;
strsupply:=frminputgoods.inputgoodssupply;
strvip:=frminputgoods.inputgoodsvip;
//strtxm:=frminputgoods.inputgoodstxm;
thisnode:=treeview1.Selected;
str:=trim(treeview1.Selected.Text);
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select top 1 code from h_forgoods where rtrim(ltrim(type))='''+trim(str)+'''');
adoquery1.Open;
code1:=trim(adoquery1.FieldByName('code').AsString);
//code1:=adoquery1.FieldValues['code'];
try
adoquery2.Connection.begin
Trans;
adoquery2.Close;
adoquery2.SQL.Clear;
adoquery2.SQL.Add('insert into h_goods(g_code,g_num,g_name,g_dw,g_snum,g_dz) values('''+trim(code1)+''','''+trim(strnum)+''','''+trim(strname)+''','''+trim(strdw)+''','''+trim(strsupply)+''','''+trim(floattostr(strvip))+''')');
adoquery2.ExecSQL;
adoquery2.Connection.CommitTrans;
except
on e:exceptiondo
begin
showmessage(e.Message);
adoquery2.Connection.RollbackTrans;
end;
end;
adoquery2.Close;
adoquery2.SQL.Clear;
adoquery2.SQL.Add('select * from h_goods where rtrim(ltrim(g_code))='''+trim(code1)+'''');
adoquery2.Open
end;
end;
{adoquery2.Close;
adoquery2.SQL.Clear;
adoquery2.SQL.Add('select * from h_goods');
adoquery2.Open;
adoquery2.Insert;
adoquery2.FieldByName('g_code').AsInteger:=code;
adoquery2.Post;}
procedure Tfrmgoods.N5Click(Sender: TObject);
begin
try
if application.MessageBox('您是否想删除该记录?','删除记录',mb_yesno+MB_ICONQUESTION+MB_DEFBUTTON2)=idyes then
adoquery2.Delete;
except
on e:exceptiondo
showmessage(e.Message);
end;
end;
procedure Tfrmgoods.N6Click(Sender: TObject);
begin
try
adoquery2.Edit;
except
on e:exceptiondo
showmessage(e.Message);
end;
end;
procedure Tfrmgoods.Button1Click(Sender: TObject);
begin
try
adoquery2.UpdateBatch(arall);
except
on e:exceptiondo
showmessage(e.Message);
end;
end;
procedure Tfrmgoods.Button2Click(Sender: TObject);
begin
try
adoquery2.CancelBatch(arall);
except
on e:exceptiondo
showmessage(e.Message);
end;
end;
procedure Tfrmgoods.DBGrid1DrawColumnCell(Sender: TObject;
const Rect: TRect;
DataCol: Integer;
Column: TColumn;
State: TGridDrawState);
var
i:integer;
begin
if (gdFocused in State) then
begin
if (COLUMN.FieldName = DBComboBox1.DataField ) then
begin
DBComboBox1.Left := Rect.Left + DBGrid1.Left;
DBComboBox1.Top := Rect.Top + DBGrid1.top;
DBComboBox1.Width := Rect.Right - Rect.Left+2;
DBComboBox1.Height := Rect.Bottom - Rect.Top;
DBComboBox1.Visible := True;
end;
end;
for i:=0 to dbgrid1.Columns.Count-1do
begin
dbgrid1.Columns.Title.Color:=clbtnhighlight;
dbgrid1.Columns.Title.Font.Color:=clred;
dbgrid1.Columns.Title.Font.Style:=[fsbold];
dbgrid1.Columns.Alignment:=tacenter;
end;
DBGRID1.DefaultDrawColumnCell(RECT,DATACOL,COLUMN,STATE);
if gdselected IN State then
begin
dbgrid1.Canvas.brush.color:=rgb(220,150,150);
dbgrid1.Canvas.FillRect(rect);
dbgrid1.Canvas.font.color:=clblack;
dbgrid1.Canvas.TextOut(rect.left,rect.top,column.Field.AsString);
end;
end;
procedure Tfrmgoods.DBGrid1KeyPress(Sender: TObject;
var Key: Char);
begin
if (key <> chr(9)) then
begin
if (DBGrid1.SelectedField.FieldName=DBComboBox1.DataField) then
begin
DBComboBox1.SetFocus;
SendMessage(DBComboBox1.Handle, WM_Char, word(Key), 0);
end;
end;
end;
procedure Tfrmgoods.DBGrid1ColExit(Sender: TObject);
begin
If DBGrid1.SelectedField.FieldName = DBComboBox1.DataField then
begin
DBComboBox1.Visible := FALSE;
end;
end;
procedure Tfrmgoods.FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
begin
canclose:=true;
if frmgoods.adoupdatespending(adoquery2) then
canclose:=(application.MessageBox('更新还在进行中,如果您关闭窗口,会破坏数据的统一!','关闭窗口',mb_yesno+MB_ICONQUESTION+MB_DEFBUTTON2)=idyes);
end;
function tfrmgoods.adoupdatespending(adoquery:tcustomadodataset):boolean;
var
clone:tadoquery;
begin
try
clone:=tadoquery.Create(nil);
clone.Clone(adoquery);
clone.FilterGroup:=fgpendingrecords;
clone.Filtered:=true;
result:=not(clone.Eof and clone.Bof);
clone.Close;
finally
clone.Free;
end;
end;
procedure Tfrmgoods.Button4Click(Sender: TObject);
begin
goodsnum:=trim(adoquery2.FieldByName('g_num').AsString);
goodsname:=trim(adoquery2.FieldByName('g_name').AsString);
//goodstxm:=adoquery2.FieldByName('g_txm').AsString;
goodsdw:=trim(adoquery2.FieldByName('g_dw').AsString);
goodssnum:=trim(adoquery2.FieldByName('g_snum').AsString);
goodsvip:=adoquery2.FieldByName('g_dz').AsFloat;
//showmessage(trim(goodsnum)+trim(goodsname)+trim(goodsdw)+trim(goodsnum)+trim(floattostr(goodsvip)));
end;
procedure Tfrmgoods.Button5Click(Sender: TObject);
var
btnviable:tpublic;
begin
try
btnviable:=tpublic.Create;
btnviable.movefirst(adoquery2);
btnviable.buttonvisable(adoquery2,button5,button6,button7,button8);
finally
btnviable.Free
end;
end;
procedure Tfrmgoods.Button6Click(Sender: TObject);
var
btnviable:tpublic;
begin
try
btnviable:=tpublic.Create;
btnviable.moveprior(adoquery2);
btnviable.buttonvisable(adoquery2,button5,button6,button7,button8);
finally
btnviable.Free
end;
end;
procedure Tfrmgoods.Button7Click(Sender: TObject);
var
btnviable:tpublic;
begin
try
btnviable:=tpublic.Create;
btnviable.movelast(adoquery2);
btnviable.buttonvisable(adoquery2,button5,button6,button7,button8);
finally
btnviable.Free
end;
end;
procedure Tfrmgoods.Button8Click(Sender: TObject);
var
btnviable:tpublic;
begin
try
btnviable:=tpublic.Create;
btnviable.movenext(adoquery2);
btnviable.buttonvisable(adoquery2,button5,button6,button7,button8);
finally
btnviable.Free
end;
end;
procedure Tfrmgoods.FormShow(Sender: TObject);
var
str:string;
begin
dbcombobox1.Items.Clear;
adoquery3.Close;
adoquery3.SQL.Clear;
adoquery3.SQL.Add('select distinct s_name from h_supply');
adoquery3.Open;
adoquery3.First;
while not adoquery3.Eofdo
begin
str:=trim(adoquery3.FieldByName('s_name').AsString);
dbcombobox1.Items.Add(str);
adoquery3.Next;
end;
dbcombobox1.Visible:=false;
end;
end.