能写这样的代码能值多少大元。或者说接下来还得学些什么?(有点迷茫,因为大部分在写数据库程序)(83分)

  • 主题发起人 主题发起人 hzjone
  • 开始时间 开始时间
H

hzjone

Unregistered / Unconfirmed
GUEST, unregistred user!
以下是一个无限的在树型中插入记录。基中移动按钮与正常插入删除操作按钮封装在一个类中。打算把它做成一个COM服务器。表单以继承方式书写。请指教。
procedure Tfrmgoods.FormActivate(Sender: TObject);
var
thisnode:ttreenode;
lstype,lscode:string;
a:boolean;
buttonvisable:tpublic;
begin
a:=true;
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select type,code from h_forgoods where pcode=0');
adoquery1.Open;
adoquery1.First;
thisnode:=treeview1.Items.GetFirstNode;
thisnode.DeleteChildren;
while not (adoquery1.Eof)do
begin
lstype:=trim(adoquery1.FieldByName('type').AsString);
lscode:=trim(adoquery1.FieldByName('code').AsString);
thisnode:=treeview1.Items.Addchild(thisnode,lstype);
adoquery3.Close;
adoquery3.SQL.Clear;
adoquery3.SQL.add('select type from h_forgoods where pcode='''+lscode+'''');
adoquery3.Open;
adoquery3.First;
while not adoquery3.Eofdo
begin
lstype:=trim(adoquery3.FieldByName('type').AsString);
treeview1.Items.Addchild(thisnode,lstype);
adoquery3.Next;
end;
thisnode:=treeview1.Items.GetFirstNode;
adoquery1.Next;
end;
adoquery2.Close;
adoquery2.SQL.Clear;
adoquery2.SQL.Add('select * from h_goods where g_code=0');
adoquery2.Open;
//treeview1.FullExpand;
treeview1.Selected:=treeview1.Items.GetFirstNode;
try
buttonvisable:=tpublic.Create;
buttonvisable.state (adoquery2,operation1.spedit,operation1.spcopy,operation1.spinsert,operation1.spdelete ,operation1.spsave,operation1.spcancel,operation1.spprint,operation1.spexcell);//刷新按钮的状态
finally
buttonvisable.Free;
end;
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.FieldByName('code').AsInteger;
code1:=inttostr(code);
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select max(code) as max from h_forgoods');//获得h_forgoods的code最大值
adoquery1.Open;
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:exceptiondo
begin
showmessage(e.Message);
adotable1.Connection.RollbackTrans;
end;
end;
end;
end;

function tfrmgoods.checkcf(str:string;str1:string):boolean;
begin
checkcf:=false;
frmatype.Caption:=str;//更改标题
frmatype.Edit1.Text:=str1;
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.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.FieldByName('child').AsInteger;
//得到该结点是否有孩子
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
adoquery1.Connection.RollbackTrans;
showmessage(e.Message);
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,str) 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.Insert;
adoquery2.FieldByName('g_code').Value:=trim(code1);
adoquery2.FieldByName('g_num').Value:=trim(strnum);
adoquery2.FieldByName('g_name').Value:=trim(strname);
adoquery2.FieldByName('g_dw').Value:=trim(strdw);
adoquery2.FieldByName('g_snum').Value:=trim(strsupply);
adoquery2.FieldByName('g_dz').Value:=trim(floattostr(strvip));
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;

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
dbgrid1.ReadOnly:=false;
adoquery2.Edit;
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+2;
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:=clblack;
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(255,0,0);
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.FormShow(Sender: TObject);
var
str:string;
begin
dbgrid1.ReadOnly:=true;
dbcombobox1.ReadOnly:=true;
operation1.spcopy.Visible:=false;
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;
procedure Tfrmgoods.TreeView1DblClick(Sender: TObject);
begin
{
var
thisnode,thisnode1:ttreenode;
str,str1,lscode,lstype:string;
begin
if treeview1.Selected<>treeview1.Items.GetFirstNode then
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;
thisnode1:=thisnode;
while not (adoquery1.Eof)do
begin
str:=trim(adoquery1.FieldByName('type').AsString);
thisnode:=thisnode1;
thisnode:=treeview1.Items.Addchild(thisnode,str);
lscode:=str1;
adoquery3.Close;
adoquery3.SQL.Clear;
adoquery3.SQL.add('select type from h_forgoods where pcode='''+lscode+'''');
adoquery3.Open;
adoquery3.First;
while not adoquery3.Eofdo
begin
lstype:=trim(adoquery3.FieldByName('type').AsString);
treeview1.Items.Addchild(thisnode,lstype);
adoquery3.Next;
end;
adoquery1.Next;
end;
adoquery2.Close;
adoquery2.SQL.Clear;
adoquery2.SQL.Add('select * from h_goods where g_code='''+str1+'''');
adoquery2.Open;
end;
}
end;

procedure Tfrmgoods.TreeView1Expanding(Sender: TObject;
Node: TTreeNode;
var AllowExpansion: Boolean);
var
thisnode,thisnode1:ttreenode;
lscode,lscode1,lstype:string;
begin
if node<>treeview1.Items.GetFirstNode then
begin
node.DeleteChildren;
lstype:=node.Text;
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select top 1 code from h_forgoods where rtrim(ltrim(type))='''+lstype+'''');
adoquery1.Open;
lscode:=trim(adoquery1.FieldByName('code').AsString);
lscode1:=lscode;
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select type,code from h_forgoods where rtrim(ltrim(pcode))='''+trim(lscode)+'''');
adoquery1.Open;
adoquery1.First;
//thisnode1:=thisnode;
while not (adoquery1.Eof)do
begin
lstype:=trim(adoquery1.FieldByName('type').AsString);
//thisnode:=thisnode1;
thisnode:=treeview1.Items.Addchild(node,lstype);
lscode:=trim(adoquery1.FieldByName('code').AsString);
adoquery3.Close;
adoquery3.SQL.Clear;
adoquery3.SQL.add('select type from h_forgoods where pcode='''+lscode+'''');
adoquery3.Open;
adoquery3.First;
while not adoquery3.Eofdo
begin
lstype:=trim(adoquery3.FieldByName('type').AsString);
treeview1.Items.Addchild(thisnode,lstype);
adoquery3.Next;
end;
adoquery1.Next;
end;
end;
adoquery2.Close;
adoquery2.SQL.Clear;
adoquery2.SQL.Add('select * from h_goods where g_code='''+lscode1+'''');
adoquery2.Open;
end;

procedure Tfrmgoods.Frame11spfirstClick(Sender: TObject);
begin
frame11.dataset:=adoquery2;
Frame11.spfirstClick(Sender);
end;

procedure Tfrmgoods.Frame11sppriorClick(Sender: TObject);
begin
frame11.dataset:=adoquery2;
Frame11.sppriorClick(Sender);
end;

procedure Tfrmgoods.Frame11spnextClick(Sender: TObject);
begin
frame11.dataset:=adoquery2;
Frame11.spnextClick(Sender);
end;

procedure Tfrmgoods.Frame11splastClick(Sender: TObject);
begin
frame11.dataset:=adoquery2;
Frame11.splastClick(Sender);
end;

procedure Tfrmgoods.speditClick(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.operation1speditClick(Sender: TObject);
begin
try
operation1.ADODATASET:=adoquery2;
operation1.dbgrid:=dbgrid1;
operation1.speditClick(Sender);
dbcombobox1.ReadOnly:=false;
except
on e:edatabaseerrordo
adoerror(DM.ADOCon,e);
ON E:EOleErrordo
OleError(E);
end;
end;

procedure Tfrmgoods.operation1spcopyClick(Sender: TObject);
begin
try
operation1.ADODATASET:=adoquery2;
operation1.dbgrid:=dbgrid1;
operation1.spcopyClick(Sender);
except
on e:edatabaseerrordo
adoerror(DM.ADOCon,e);
ON E:EOleErrordo
OleError(E);
end;
end;
procedure Tfrmgoods.operation1spinsertClick(Sender: TObject);
var
lscode:string;
lstype:string;
begin
try
operation1.ADODATASET:=adoquery2;
operation1.dbgrid:=dbgrid1;
//buttonvisable.state (adoquery2,operation1.spedit,operation1.spcopy,operation1.spinsert,operation1.spdelete ,operation1.spsave,operation1.spcancel,operation1.spprint,operation1.spexcell);//刷新按钮的状态
operation1.spinsertClick(Sender);
dbcombobox1.ReadOnly:=false;
lstype:=treeview1.Selected.Text;
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select top 1 code from h_forgoods where rtrim(ltrim(type))='''+trim(lstype)+'''');
adoquery1.Open;
lscode:=trim(adoquery1.FieldByName('code').AsString);
adoquery2.FieldByName('g_code').Value:=lscode
except
on e:edatabaseerrordo
adoerror(DM.ADOCon,e);
ON E:EOleErrordo
OleError(E);
end;
end;

procedure Tfrmgoods.operation1spdeleteClick(Sender: TObject);
begin
try
operation1.ADODATASET:=adoquery2;
operation1.dbgrid:=dbgrid1;
operation1.spdeleteClick(Sender);
dbcombobox1.ReadOnly:=false;
except
on e:edatabaseerrordo
adoerror(DM.ADOCon,e);
ON E:EOleErrordo
OleError(E);
end;
end;

procedure Tfrmgoods.operation1spsaveClick(Sender: TObject);
begin
try
operation1.ADODATASET:=adoquery2;
operation1.dbgrid:=dbgrid1;
operation1.spsaveClick(Sender);
dbcombobox1.ReadOnly:=true;
except
on e:edatabaseerrordo
adoerror(DM.ADOCon,e);
ON E:EOleErrordo
OleError(E);
end;
end;

procedure Tfrmgoods.operation1spcancelClick(Sender: TObject);
begin
try
operation1.ADODATASET:=adoquery2;
operation1.dbgrid:=dbgrid1;
operation1.spcancelClick(Sender);
dbcombobox1.ReadOnly:=true;
except
on e:edatabaseerrordo
adoerror(DM.ADOCon,e);
ON E:EOleErrordo
OleError(E);
end;
end;

procedure Tfrmgoods.operation1spexcellClick(Sender: TObject);
begin
operation1.ADODATASET:=adoquery2;
operation1.dbgrid:=dbgrid1;
operation1.spexcellClick(Sender);
end;

procedure Tfrmgoods.operation1spcloseClick(Sender: TObject);
begin
close;
end;

procedure Tfrmgoods.spselectClick(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;
end;

procedure Tfrmgoods.TreeView1Click(Sender: TObject);
var
lstype,lscode:string;
buttonvisable:tpublic;
begin
lstype:=treeview1.Selected.Text;
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select top 1 code from h_forgoods where rtrim(ltrim(type))='''+lstype+'''');
adoquery1.Open;
lscode:=adoquery1.FieldByName('code').Value;
adoquery2.Close;
adoquery2.SQL.Clear;
adoquery2.SQL.Add('select * from h_goods where g_code='''+lscode+'''');
adoquery2.Open;
try
buttonvisable:=tpublic.Create;
buttonvisable.state (adoquery2,operation1.spedit,operation1.spcopy,operation1.spinsert,operation1.spdelete ,operation1.spsave,operation1.spcancel,operation1.spprint,operation1.spexcell);//刷新按钮的状态
finally
buttonvisable.Free;
end;
end;
 
对数据库的操作大部分都在界面上,不利于维护&amp;阅读。
看看《delphi面向对象思想》中的“界面和代码分离”
 
你现在拿的是多少呢?
 
to mastar:你猜我能拿多少啊?
TO JLUTT-SADAN:在哪里有这本书啊。
 
书店有这本书买,你应该买,不会亏的。拿多少还要看地方,我想说的是,你的CODE风格好象不很好,阅读起来比较吃力。
 
呵呵,我不建议你买这本书,太泛泛了.
建议你买<重构>这本书,就知道你的代码存在什么问题了.
 
看 代码大全 ,学习 UML 和 OOP 知识,做做控件。
 
谢谢MSTAR。但总得给我一个价。
 
我想从熟手到高手 你还有个过程 我发现 水平也是高明的 代码越少
 
比如说 你这个代码 如果针对 TREEVIEW 的 DATA 属性使用 结构和指针 就会漂亮多了
 
能把这么多代码看完的我都服了。看了一眼我都有点眼花
 
晕,这样的代码时间长了,你自己看着也费劲吧?
 
to mstar:
可以给我一小段代码。让我借鉴一下吗?我的MAIL是:hzjone@hotmail.com
to lyplay2:
可能我熟悉吧。我看起来还可以的。我命名做得很不好。现在正在改正。
 
The following code defines a record type of TMyRec and a record pointer type of PMyRec.
type
PMyRec = ^TMyRec;
TMyRec = record
FName: string;
LName: string;
end;

Assuming these types are used, the following code adds a node to TreeView1 as the last sibling of a specified node. A TMyRec record is associated with the added item. The FName and LName fields are obtained from edit boxes Edit1 and Edit2. The Index parameter is obtained from edit box Edit3. The item is added only if the Index is a valid value.
procedure TForm1.Button1Click(Sender: TObject);
var
MyRecPtr: PMyRec;
TreeViewIndex: LongInt;
begin
New(MyRecPtr);
MyRecPtr^.FName := Edit1.Text;
MyRecPtr^.LName := Edit2.Text;
TreeViewIndex := StrToInt(Edit3.Text);
with TreeView1do
begin
if Items.Count = 0 then
Items.AddObject(nil, 'Item' + IntToStr(TreeViewIndex), MyRecPtr)
else
if (TreeViewIndex < Items.Count) and (TreeViewIndex >= 0) then

Items.AddObject(Items[TreeViewIndex], 'Item' + IntToStr(TreeViewIndex), MyRecPtr);
end;
end;

After an item containing a TMyRec record has been added, the following code retrieves the FName and LName values associated with the item and displays the values in a label.
procedure TForm1.Button2Click(Sender: TObject);
begin
Label1.Caption := PMyRec(TreeView1.Selected.Data)^.FName + ' ' +
PMyRec(TreeView1.Selected.Data)^.LName;
end;
 
多人接受答案了。
 
后退
顶部