procedure TForm1.Button1Click(Sender: TObject);//插入同一级目录 var p_data:ttreenode; begin p_data:=treeview1.Selected; treeview1.Items.AddObject(p_data,edit1.text,nil); end;procedure TForm1.Button2Click(Sender: TObject);//插入子目录 var p_data:ttreenode; begin p_data:=treeview1.Selected; treeview1.Items.AddChildObject(p_data,edit1.text,nil); end;
Add可以插入(added as the last sibling of the Node parameter),但不能在中间插入。它的说明如下(Delphi Help) Adds a new tree node to a tree view control.function Add(Node: TTreeNode; const S: string): TTreeNode;DescriptionThe node is added as the last sibling of the Node parameter. The S parameter specifies the Text property of the new node. Add returns the node that has been added. If the tree view is sorted, Add inserts the node in the correct sort order position rather than as the last child of the Node parameter抯 parent.
var i,j:integer; begin for i:=1 to 5 do begin node:=Treeview1.Items.Add(Treeview1.Selected,inttostr(i)); node.ImageIndex:=2; for j:=1 to 3 do begin Treeview1.Items.AddChild(node,inttostr(i)+inttostr(j)).imageindex:=0; end; end; TreeView1.Selected:=nil;
treeview1.Items.Insert(Node,'name');
var
p_data:ttreenode;
begin
p_data:=treeview1.Selected;
treeview1.Items.AddObject(p_data,edit1.text,nil);
end;procedure TForm1.Button2Click(Sender: TObject);//插入子目录
var
p_data:ttreenode;
begin
p_data:=treeview1.Selected;
treeview1.Items.AddChildObject(p_data,edit1.text,nil);
end;
问题可以解决了。太感谢大家的帮助了。给分...!
Adds a new tree node to a tree view control.function Add(Node: TTreeNode; const S: string): TTreeNode;DescriptionThe node is added as the last sibling of the Node parameter. The S parameter specifies the Text property of the new node. Add returns the node that has been added. If the tree view is sorted, Add inserts the node in the correct sort order position rather than as the last child of the Node parameter抯 parent.
i,j:integer;
begin
for i:=1 to 5 do
begin
node:=Treeview1.Items.Add(Treeview1.Selected,inttostr(i));
node.ImageIndex:=2;
for j:=1 to 3 do
begin
Treeview1.Items.AddChild(node,inttostr(i)+inttostr(j)).imageindex:=0;
end;
end;
TreeView1.Selected:=nil;