如何删除树节点并将其节点节点向上移动?

发布于 2024-11-05 06:18:39 字数 103 浏览 0 评论 0原文

实际上在我的树视图中,当我删除一个树节点时,它会删除它的所有子节点,但我需要向上移动它的子节点而不是删除。我必须在c Sharp的winforms中使用。

任何人都可以帮帮我。

Actually in my treeview ,when i remove a tree node it removes all its child node, But i need to move its child node upwards instead of removing .I have to use in winforms in c sharp.

Anybody help me out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沙沙粒小 2024-11-12 06:18:39

那么您只想删除一个节点并保留任何子节点?

您要做的是:

  1. 从要删除的节点中删除子节点。
  2. 在要删除的节点之前添加子节点。
  3. 删除选定的节点。

So you just want to remove a node and keep any childnodes?

What you have to do is:

  1. Remove childnodes from the node you want to delete.
  2. Add childnodes before the node you want to delete.
  3. Remove the selected node.
凶凌 2024-11-12 06:18:39

狂想曲说的话。
这是一个例子:

if (tree.Nodes.Contains(theNode))
        {
            TreeNodeCollection childNodes = theNode.Nodes;
            tree.Nodes.Remove(theNode);
            foreach (TreeNode child in childNodes)
            {
                tree.Nodes.Add(child);
            }
        }

What Rhapsody said.
And here is an example:

if (tree.Nodes.Contains(theNode))
        {
            TreeNodeCollection childNodes = theNode.Nodes;
            tree.Nodes.Remove(theNode);
            foreach (TreeNode child in childNodes)
            {
                tree.Nodes.Add(child);
            }
        }
暖风昔人 2024-11-12 06:18:39

您遇到的问题是由任何给定节点的子节点存储在 myNode.Nodes 中这一事实引起的。因此,当您删除一个节点时,它的所有节点也会被释放,因此您必须首先迭代子节点,移动它们,然后删除原始节点:

//assume treeChild is what you are removing, and treeControl is you TreeView
//this code will move all of its children nodes
//to be children of its parent before being removed

//set that we are updating the treeview control
//increases performance by blocking the paint methods until update is finished
treeControl.BeginUpdate();

//this will loop through child nodes of what we are removing
//then add them to the parent
foreach(TreeView node in treeChild.ChildNodes)
{
   node.Parent.Nodes.Add(node);
}

//then remove the node
treeChild.Remove();

treeControl.EndUpdate(); //note that we finished updated the controls

The problem you're having is caused by the fact that the child nodes of any given node is stored in myNode.Nodes. So, when you remove a node all of its nodes are freed as well, so you will have to first iterate through the child nodes, move them, then remove the original node:

//assume treeChild is what you are removing, and treeControl is you TreeView
//this code will move all of its children nodes
//to be children of its parent before being removed

//set that we are updating the treeview control
//increases performance by blocking the paint methods until update is finished
treeControl.BeginUpdate();

//this will loop through child nodes of what we are removing
//then add them to the parent
foreach(TreeView node in treeChild.ChildNodes)
{
   node.Parent.Nodes.Add(node);
}

//then remove the node
treeChild.Remove();

treeControl.EndUpdate(); //note that we finished updated the controls
雨落星ぅ辰 2024-11-12 06:18:39

您可以循环遍历子节点并将它们添加到节点的父节点,然后再删除节点本身。此代码应该处理要删除的节点是父节点的情况。

if (nodeToRemove.Nodes.Count > 0) {
List<TreeNode> childNodes = new List<TreeNode>();
foreach (TreeNode n in nodeToRemove.Nodes) {
   childNodes.Add(n);
}
if ((nodeToRemove.Parent != null)) {
   nodeToRemove.Parent.Nodes.AddRange(childNodes.ToArray());
   } else {
     nodeToRemove.TreeView.Nodes.AddRange(childNodes.ToArray());
    }
   }
nodeToRemove.Remove();

You can loop through the child nodes and add them to the node's parent before removing the node itself. This code should handle cases where the node to be removed is a parent node.

if (nodeToRemove.Nodes.Count > 0) {
List<TreeNode> childNodes = new List<TreeNode>();
foreach (TreeNode n in nodeToRemove.Nodes) {
   childNodes.Add(n);
}
if ((nodeToRemove.Parent != null)) {
   nodeToRemove.Parent.Nodes.AddRange(childNodes.ToArray());
   } else {
     nodeToRemove.TreeView.Nodes.AddRange(childNodes.ToArray());
    }
   }
nodeToRemove.Remove();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文