N
nowbelle
Unregistered / Unconfirmed
GUEST, unregistred user!
看了刘艺老师DELPHI面向对象编程思想后,很有感触,想把自己的程序改进一下,但是在关于Treeview界面和业务分离上出现了问题,
原来的代码是在FORMSHOW里面创建Treeview里的内容具体如下:
//初始化TREE
dm.ADOQuery1.Close;
dm.ADOQuery1.SQL.Clear;
dm.ADOQuery1.SQL.Add('select fieldSetDesc,fieldSetId from fieldSet where fieldSetId like ''A%''');
dm.ADOQuery1.Open;
while not dm.ADOQuery1.Eof do
begin
treetext:=dm.ADOQuery1.FieldByName('fieldSetDesc').AsString;
tabname:=dm.ADOQuery1.FieldByName('fieldSetId').AsString;
node:=Tree.Items.Add(Tree.TopItem,treetext);
dm.ADOQuery2.Close;
dm.ADOQuery2.SQL.Clear;
dm.ADOQuery2.SQL.Add('select itemdesc from fielditem where fieldSetId ='''+tabname+''' and useflag=''1''');
dm.ADOQuery2.Open;
while not dm.ADOQuery2.Eof do
begin
treetext:=dm.ADOQuery2.FieldByName('itemdesc').AsString;
Tree.Items.AddChild(node,treetext);
dm.ADOQuery2.Next;
end;
dm.ADOQuery1.Next;
end;
将这块分离后创建了三个模块,一个DM(DataModule),一个FORM1(界面层),一个U_Maint(业务层,这个是新建的一个Unit,不是FORM)。
DataModule里面放了一个ADOConnection,两个ADOQuery
U_Maint里的代码如下:
unit U_Maint;
interface
uses
Windows, Messages, SysUtils, Variants, Classes,ComCtrls,ADODB,Unit_DM;
type
TMaint= class(TObject)
private
UseDM:TDM;
public
function FieldTree(TopNode:TTreeNode):TTreeNodes;
constructor create;
destructor Destroy;override;
end;
implementation
{ TMaint}
constructor TMaint.create;
begin
UseDM:=TDM.Create(nil);
end;
destructor TMaint.Destroy;
begin
FreeAndNil(UseDM);
inherited;
end;
function TMaint.FieldTree(TopNode:TTreeNode): TTreeNodes;
var
treetext,tabname:string;
nodes:TTreeNodes;
node:TTreeNode;
begin
//初始化
UseDM.ADOQuery1.Close;
UseDM.ADOQuery1.SQL.Clear;
UseDM.ADOQuery1.SQL.Add('select fieldSetDesc,fieldSetId from fieldSet where fieldSetId like ''A%''');
UseDM.ADOQuery1.Open;
while not UseDM.ADOQuery1.Eof do
begin
treetext:=UseDM.ADOQuery1.FieldByName('fieldSetDesc').AsString;
tabname:=UseDM.ADOQuery1.FieldByName('fieldSetId').AsString;
node:=nodes.Add(TopNode,treetext);
UseDM.ADOQuery2.Close;
UseDM.ADOQuery2.SQL.Clear;
UseDM.ADOQuery2.SQL.Add('select itemdesc from fielditem where fieldSetId ='''+tabname+''' and useflag=''1''');
UseDM.ADOQuery2.Open;
while not UseDM.ADOQuery2.Eof do
begin
treetext:=UseDM.ADOQuery2.FieldByName('itemdesc').AsString;
nodes.AddChild(node,treetext);
UseDM.ADOQuery2.Next;
end;
UseDM.ADOQuery1.Next;
end;
Result:=nodes;
end;
end.
最后在FORM1中拖入一个Treeview控件起名tree,在private下添加objectTree:TMaint;
在FORMSHOW里添加
tree.Items.Assign(objectTree.FieldTree(tree.TopItem));
编译成功,但运行后会出错,不知道什么原因怎么修改,请各位高手指教~顺便想请教各位高手有没有更好的分离方法,小弟不胜感激~
原来的代码是在FORMSHOW里面创建Treeview里的内容具体如下:
//初始化TREE
dm.ADOQuery1.Close;
dm.ADOQuery1.SQL.Clear;
dm.ADOQuery1.SQL.Add('select fieldSetDesc,fieldSetId from fieldSet where fieldSetId like ''A%''');
dm.ADOQuery1.Open;
while not dm.ADOQuery1.Eof do
begin
treetext:=dm.ADOQuery1.FieldByName('fieldSetDesc').AsString;
tabname:=dm.ADOQuery1.FieldByName('fieldSetId').AsString;
node:=Tree.Items.Add(Tree.TopItem,treetext);
dm.ADOQuery2.Close;
dm.ADOQuery2.SQL.Clear;
dm.ADOQuery2.SQL.Add('select itemdesc from fielditem where fieldSetId ='''+tabname+''' and useflag=''1''');
dm.ADOQuery2.Open;
while not dm.ADOQuery2.Eof do
begin
treetext:=dm.ADOQuery2.FieldByName('itemdesc').AsString;
Tree.Items.AddChild(node,treetext);
dm.ADOQuery2.Next;
end;
dm.ADOQuery1.Next;
end;
将这块分离后创建了三个模块,一个DM(DataModule),一个FORM1(界面层),一个U_Maint(业务层,这个是新建的一个Unit,不是FORM)。
DataModule里面放了一个ADOConnection,两个ADOQuery
U_Maint里的代码如下:
unit U_Maint;
interface
uses
Windows, Messages, SysUtils, Variants, Classes,ComCtrls,ADODB,Unit_DM;
type
TMaint= class(TObject)
private
UseDM:TDM;
public
function FieldTree(TopNode:TTreeNode):TTreeNodes;
constructor create;
destructor Destroy;override;
end;
implementation
{ TMaint}
constructor TMaint.create;
begin
UseDM:=TDM.Create(nil);
end;
destructor TMaint.Destroy;
begin
FreeAndNil(UseDM);
inherited;
end;
function TMaint.FieldTree(TopNode:TTreeNode): TTreeNodes;
var
treetext,tabname:string;
nodes:TTreeNodes;
node:TTreeNode;
begin
//初始化
UseDM.ADOQuery1.Close;
UseDM.ADOQuery1.SQL.Clear;
UseDM.ADOQuery1.SQL.Add('select fieldSetDesc,fieldSetId from fieldSet where fieldSetId like ''A%''');
UseDM.ADOQuery1.Open;
while not UseDM.ADOQuery1.Eof do
begin
treetext:=UseDM.ADOQuery1.FieldByName('fieldSetDesc').AsString;
tabname:=UseDM.ADOQuery1.FieldByName('fieldSetId').AsString;
node:=nodes.Add(TopNode,treetext);
UseDM.ADOQuery2.Close;
UseDM.ADOQuery2.SQL.Clear;
UseDM.ADOQuery2.SQL.Add('select itemdesc from fielditem where fieldSetId ='''+tabname+''' and useflag=''1''');
UseDM.ADOQuery2.Open;
while not UseDM.ADOQuery2.Eof do
begin
treetext:=UseDM.ADOQuery2.FieldByName('itemdesc').AsString;
nodes.AddChild(node,treetext);
UseDM.ADOQuery2.Next;
end;
UseDM.ADOQuery1.Next;
end;
Result:=nodes;
end;
end.
最后在FORM1中拖入一个Treeview控件起名tree,在private下添加objectTree:TMaint;
在FORMSHOW里添加
tree.Items.Assign(objectTree.FieldTree(tree.TopItem));
编译成功,但运行后会出错,不知道什么原因怎么修改,请各位高手指教~顺便想请教各位高手有没有更好的分离方法,小弟不胜感激~