对不起刚才我写错了。因为很长时间不用了。应该这样写 var MyTreeNode1, MyTreeNode2: TTreeNode; begin with TreeView1.Items do begin Clear; { remove any existing nodes } MyTreeNode1 := Add(nil, 'RootTreeNode1'); { Add a root node } { Add a child node to the node just added } AddChild(MyTreeNode1,'ChildNode1'); {Add another root node} MyTreeNode2 := Add(MyTreeNode1, 'RootTreeNode2'); {Give MyTreeNode2 to a child } AddChild(MyTreeNode2,'ChildNode2'); {Change MyTreeNode2 to ChildNode2 } { and add a child node to it} MyTreeNode2 := TreeView1.Items[3]; AddChild(MyTreeNode2,'ChildNode2a'); {Add another child to ChildNode2, after ChildNode2a } Add(MyTreeNode2,'ChildNode2b'); {add another root node} Add(MyTreeNode1, 'RootTreeNode3'); end;这是添加删除delete就可以了
var HT : THitTests; begin if (CheckBox1.Checked) and (Sender is TTreeView) then begin with Sender as TTreeView do begin HT := GetHitTestInfoAt(X,Y); if (htOnItem in HT) then Items.Delete(GetNodeAt(X,Y)); end; end; 删除可以在mousedown中进行。
TreeView.items[].delete
不知是不是这样写很长时间不用了。好象还有一个child属性也要用上。
var
MyTreeNode1, MyTreeNode2: TTreeNode;
begin
with TreeView1.Items do
begin
Clear; { remove any existing nodes }
MyTreeNode1 := Add(nil, 'RootTreeNode1'); { Add a root node }
{ Add a child node to the node just added }
AddChild(MyTreeNode1,'ChildNode1'); {Add another root node}
MyTreeNode2 := Add(MyTreeNode1, 'RootTreeNode2');
{Give MyTreeNode2 to a child }
AddChild(MyTreeNode2,'ChildNode2'); {Change MyTreeNode2 to ChildNode2 }
{ and add a child node to it}
MyTreeNode2 := TreeView1.Items[3];
AddChild(MyTreeNode2,'ChildNode2a'); {Add another child to ChildNode2, after ChildNode2a }
Add(MyTreeNode2,'ChildNode2b'); {add another root node}
Add(MyTreeNode1, 'RootTreeNode3');
end;这是添加删除delete就可以了
HT : THitTests;
begin
if (CheckBox1.Checked) and (Sender is TTreeView) then
begin
with Sender as TTreeView do
begin
HT := GetHitTestInfoAt(X,Y);
if (htOnItem in HT) then
Items.Delete(GetNodeAt(X,Y));
end;
end;
删除可以在mousedown中进行。