SORRY,上面贴的一段代码是很久以前写的,有关内容早已经忘了
今天重新试了试,发现是这样的:在TREEVIEW中,
root1 --- child1
--- child2
root2 --- child3
--- child4
root2的ITEMS索引是3,即你访问的Treeview1.items[3]
而下面两条代码是等价的,都是CHILD2
caption:=TreeView1.Items[2].text;
caption:=TreeView1.Items[0].Item[1].text;
procedure TForm1.Button3Click(Sender: TObject);
var
aNode,aNode1 : TTreeNode;
begin
// aNode:=TTreeNode.Create(TreeView1.Items);
aNode:=TreeView1.Items.Add(nil,'Root1');
aNode1:=TreeView1.Items.AddChild(aNode,'Child1');
aNode1:=TreeView1.Items.AddChild(aNode,'Child2');
aNode:=TreeView1.Items.Add(aNode,'Root2');
aNode1:=TreeView1.Items.AddChild(aNode,'Child3');
aNode1:=TreeView1.Items.AddChild(aNode,'Child4');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
// TreeView1.items[0].Expand(True);
// TreeView1.items[0].item[1].Selected:=true;
TreeView1.items[1].Expand(True);
// TreeView1.items[1].Item[1].Text:='Test';
// TreeView1.items[1].item[1].Selected:=true;
caption:=TreeView1.Items[2].text;
caption:=TreeView1.Items[0].Item[1].text;
end;
其实如果要访问节点,我认为最好还是用 GetFirstChild GetLastChild
GetNext GetNextChild GetNextSiling等方法