无
无限风
Unregistered / Unconfirmed
GUEST, unregistred user!
谁帮助我呀把下面的带码优化,把启动速度加快呀!
Adodataset1有一万多条记录
function TFrmMain.TreeAddItem(Sender: TTreeView; ItemList: TStrings;Resort: Boolean): TTreeNode;
var
ThisNode, Node: TTreeNode;
I: Integer;
begin
Node := nil; //nil = level 0 has no parent node
//this is checked by TreeFindItem
for I := 0 to Itemlist.count -1 do
begin
if i<Itemlist.count -1 then
ThisNode := TreeFindItem(Sender, node, Itemlist)
else ThisNode:=nil;
if ThisNode <> nil then
Node := ThisNode
else
begin
if I <= Itemlist.count -1 then
begin
if I = 0 then
begin
Node := Sender.items.Add(Node, Itemlist);
node.ImageIndex:=1;
end
else
begin
Node := Sender.items.AddChild(Node, Itemlist);
if i=1 then
node.ImageIndex:=1;
end;
end;
Node.stateIndex := Node.level + 1;
if Resort and (Node.parent <> nil) then Node.parent.alphasort;
end;
end;
Result := Node;
end;
function TFrmMain.TreeFindItem(Sender: TTreeView; NodeItem: TTreeNode; Name: String): TTreeNode;
begin
if NodeItem = nil then NodeItem := Sender.items.getfirstnode
else NodeItem := NodeItem.getfirstchild;
//NodeItem is now the first item of the desired level
//if this level has no items, NodeItem is nil
if (NodeItem <> nil) and (NodeItem.text <> Name) then
repeat
NodeItem := NodeItem.getnextsibling;
until (NodeItem = nil) or (NodeItem.text = Name);
Result := NodeItem;
end;
Function TFrmMain.GetFieldList: TStringList;
var str,strtem:string;
begin
FieldList.clear;
str:=Adodataset1.fieldbyname('type1').asstring;
str:=uppercase(str);
FieldList.add(str);
str:=Adodataset1.fieldbyname('nx').asstring;
strtem:=copy(str,1,2);
FieldList.add(strtem);
FieldList.add(str);
Result := FieldList;
end;
procedure TFrmMain.FormCreate(Sender: TObject);
begin
ADODataSet1.open
FieldList := TStringList.create;
TreeView1.items.BeginUpdate;
ADODataSet1.first;
while not ADODataSet1.eof do
begin
TreeAddItem(TreeView1, GetFieldList, false);
ADODataSet1.next;
end;
FieldList.clear;
TreeView1.Alphasort;
TreeView1.items.Endupdate;
ADODataSet1.first;
end;
Adodataset1有一万多条记录
function TFrmMain.TreeAddItem(Sender: TTreeView; ItemList: TStrings;Resort: Boolean): TTreeNode;
var
ThisNode, Node: TTreeNode;
I: Integer;
begin
Node := nil; //nil = level 0 has no parent node
//this is checked by TreeFindItem
for I := 0 to Itemlist.count -1 do
begin
if i<Itemlist.count -1 then
ThisNode := TreeFindItem(Sender, node, Itemlist)
else ThisNode:=nil;
if ThisNode <> nil then
Node := ThisNode
else
begin
if I <= Itemlist.count -1 then
begin
if I = 0 then
begin
Node := Sender.items.Add(Node, Itemlist);
node.ImageIndex:=1;
end
else
begin
Node := Sender.items.AddChild(Node, Itemlist);
if i=1 then
node.ImageIndex:=1;
end;
end;
Node.stateIndex := Node.level + 1;
if Resort and (Node.parent <> nil) then Node.parent.alphasort;
end;
end;
Result := Node;
end;
function TFrmMain.TreeFindItem(Sender: TTreeView; NodeItem: TTreeNode; Name: String): TTreeNode;
begin
if NodeItem = nil then NodeItem := Sender.items.getfirstnode
else NodeItem := NodeItem.getfirstchild;
//NodeItem is now the first item of the desired level
//if this level has no items, NodeItem is nil
if (NodeItem <> nil) and (NodeItem.text <> Name) then
repeat
NodeItem := NodeItem.getnextsibling;
until (NodeItem = nil) or (NodeItem.text = Name);
Result := NodeItem;
end;
Function TFrmMain.GetFieldList: TStringList;
var str,strtem:string;
begin
FieldList.clear;
str:=Adodataset1.fieldbyname('type1').asstring;
str:=uppercase(str);
FieldList.add(str);
str:=Adodataset1.fieldbyname('nx').asstring;
strtem:=copy(str,1,2);
FieldList.add(strtem);
FieldList.add(str);
Result := FieldList;
end;
procedure TFrmMain.FormCreate(Sender: TObject);
begin
ADODataSet1.open
FieldList := TStringList.create;
TreeView1.items.BeginUpdate;
ADODataSet1.first;
while not ADODataSet1.eof do
begin
TreeAddItem(TreeView1, GetFieldList, false);
ADODataSet1.next;
end;
FieldList.clear;
TreeView1.Alphasort;
TreeView1.items.Endupdate;
ADODataSet1.first;
end;