如何用新创建的节点替换 TreeView 的选定节点 [英] how to replace TreeView's selected node with newly created node

查看:32
本文介绍了如何用新创建的节点替换 TreeView 的选定节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用我创建的节点填充的树视图,有几个节点类,都继承自 treeNode.

I have a populated treeView with Node I created, there are several node classes, all inherit from treeNode.

当我编辑一个节点(使用 GUI 对话框)时,它可能会更改为不同的类,因此我在该过程中创建了一个新节点,并尝试用我的新节点替换所选节点,但这并没有t 工作,节点保持旧的,无法弄清楚我做错了什么.

When i edit a node (using a GUI dialog), it may change to different class, so I'm creating a new node in that process, and trying to replace the selected node with my new node, but that doesn't work, the node stays the old one, cant figure out what i'm doing wrong.

代码:

TreeNodeMission mission = (TreeNodeMission)treeView.SelectedNode;
TreeNodeMission newMission = ChangeMissionDialog(mission);

treeView.SelectedNode = newMission; // doesn't work

也尝试删除和添加它,也不起作用

Also tried removing and adding it, also doesn't work

index = treeView.Nodes.IndexOf(treeView.SelectedNode);  // index returns -1
treeView.Nodes.Remove(treeView.SelectedNode);
treeView.Nodes.Insert(index, newMission);

我做错了什么?

更新:treeView.SelectedNode 不为空,它是我选择的一个有效节点.

Update: treeView.SelectedNode is not null, its a valid node i selected.

推荐答案

已解决,发现错误.

我找到了一种替换节点的方法,即删除并重新添加它.我想我认为要求索引会给我树中的一般索引,但它只为父节点提供索引,所以使用父节点,我可以替换它:

I found a way to replace the node, by removing and re-adding it. I guess i thought asking for index will give me general index in the tree, but it gives index to the parent only, so using the parent node, i can replace it:

int index = treeView.SelectedNode.Index;
treeView.SelectedNode.Parent.Nodes.RemoveAt(index);
treeView.SelectedNode.Parent.Nodes.Insert(index, mission);
treeView.SelectedNode = mission;

谢谢

这篇关于如何用新创建的节点替换 TreeView 的选定节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆