delphi TreeComboBox 生成树菜单
procedure TForm11.cxbtn1Click(Sender: TObject);
var
T: DWORD;
begin
T := GetTickCount;
MakeTree(kbmMemTable1, cbb1);
lbl1.Caption := Format('MakeTree所用时间: %d ms', [GetTickCount - T]);
end;
procedure TForm11.FormCreate(Sender: TObject);
var
i, id: Integer;
begin
id := 0;
with kbmMemTable1 do
begin
DisableControls;
try
Open; //打开
for i := 0 to 5 do
begin
Inc(id);
Append; //附加数据
//赋值
FieldByName('ID').AsString := inttostr(id);
FieldByName('ParentID').AsString := inttostr(0);
FieldByName('Name').AsString := 'YXM'+'是'+inttostr(id);
Post; //确定
end;
Append; //附加数据
//赋值
FieldByName('ID').AsString := '101';
FieldByName('ParentID').AsString := '1';
FieldByName('Name').AsString := '这个是1的儿子!';
Post;
Append; //附加数据
//赋值
FieldByName('ID').AsString := '201';
FieldByName('ParentID').AsString := '2';
FieldByName('Name').AsString := '这个是2的儿子!';
Post;
Append; //附加数据
//赋值
FieldByName('ID').AsString := '301';
FieldByName('ParentID').AsString := '3';
FieldByName('Name').AsString := '这个是3的儿子!';
Post;
Append; //附加数据
//赋值
FieldByName('ID').AsString := '10101';
FieldByName('ParentID').AsString := '101';
FieldByName('Name').AsString := '101的儿子,1的孙子!';
Post;
UpdateIndexes; //更新索引
finally
EnableControls; //连接数据感知控件
end;
end;
end;
procedure TForm11.MakeTree(dataset: TDataSet; TreeComboBox: TAdvTreeComboBox); //生成树
var
List: TStringList;
Node: TTreeNode;
Index: Integer;
begin
TreeComboBox.Items.BeginUpdate;
try
TreeComboBox.Items.Clear;
List := TStringList.Create;
try
List.Sorted := True;
dataset.DisableControls;
dataset.First;
while not dataset.Eof do
begin
if dataset.FieldByName('ParentID').AsInteger = 0 then { ParentID=0,顶层节点 }
Node := TreeComboBox.Items.AddChild(nil, dataset.FieldByName('Name').AsString)
else
begin
Index := List.IndexOf(dataset.FieldByName('ParentID').AsString);
Node := TreeComboBox.Items.AddChild(TTreeNode(List.Objects[Index]),
dataset.FieldByName('Name').AsString);
end;
List.AddObject(dataset.FieldByName('ID').AsString, Node);
dataset.Next;
end;
finally
List.Free;
end;
finally
dataset.EnableControls;
TreeComboBox.Items.EndUpdate;
end;
end;
end.
参考
http://www.cnblogs.com/yplong/archive/2012/02/21/2361011.html