删除TreeView节点以及其子节点

时间:2020-12-17 06:04:42

//1.删除TreeView节点以及其子节点
procedure TForm2.Button1Click(Sender: TObject);
var TreeNode:TTreeNode;
begin
  if TreeView1.Selected <> Nil then
  Begin
    TreeNode := TreeView1.Selected.getFirstChild;
    while TreeNode <> nil do
    begin
      TreeNode.Delete;
      TreeNode := TreeView1.Selected.getFirstChild;
    end;
  end;
end;

//2.判断是否有子节点
procedure TForm2.Button2Click(Sender: TObject);
begin
  if TreeView1.Selected.HasChildren then
  begin
    ShowMessage('有子节点');
  end;
end;