VB.NET TreeView - 在插入或移动节点后动态选择节点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16502269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.NET TreeView - dynamically selecting a node after it has been inserted or moved?
提问by NotQuiteThereYet
I'm working on a VB.NET 2010 project which features a treeview control. The first thing I'm trying to figure out is how to insert a new node right after the currently selected node, and then make that newly inserted node the selectednode. I can insert the new node no problem, but I can't figure out how to make it the "selected" node. The commented line below is the part I'm getting hung up on.
我正在处理具有树视图控件的 VB.NET 2010 项目。我试图弄清楚的第一件事是如何在当前选定的节点之后插入一个新节点,然后使新插入的节点成为选定的节点。我可以插入新节点没问题,但我不知道如何使它成为“选定”节点。下面的注释行是我挂断的部分。
Private Sub AddNode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddNode.Click
If Not treeview1.SelectedNode Is Nothing Then
treeview1.Nodes.Insert(treeview1.SelectedNode.Index + 1, TextBox1.Text)
treeview.SelectedNode = treeview1.Nodes.Item(treeview1.SelectedNode.Index + 1) ' <-- I thought this would work, but it doesn't
End If
End Sub
Secondly, I am using the below code to move a selected node up (in relation to other nodes). That works fine, but similar to the problem above, I can't figure out how to keep that node as the "selected" node after it has been moved.
其次,我使用下面的代码向上移动选定的节点(相对于其他节点)。这工作正常,但与上面的问题类似,我无法弄清楚如何在移动该节点后将该节点保留为“选定”节点。
Private Sub NodeUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NodeUp.Click
Dim CurrentIndex As Integer = treeview1.SelectedNode.Index
Dim CurrentNode As TreeNode = treeview1.SelectedNode
treeview1.SelectedNode.Remove()
treeview1.Nodes.Insert(CurrentIndex - 1, CurrentNode)
End Sub
This has to be simple, but I'm wracking my brain trying to figure out how, so I would appreciate a little bit of insight here.
这必须很简单,但我正在绞尽脑汁想弄清楚如何做,所以我很感激这里的一些洞察力。
Thanks!
谢谢!
采纳答案by Adrian
For setting the selected node in a TreeView
you call TreeView.SelectedNodeto the TreeNode
you want to select.
要在 a 中设置所选节点,TreeView
请调用TreeView.SelectedNode到TreeNode
要选择的节点。
Now that we've established that, down to your examples:
既然我们已经确定了这一点,请看您的示例:
When you call TreeView.Nodes.Insertusing the overload you have (integer, string) you actually get a TreeNode
object returned to you. So if you change your sample to
当您使用您拥有的重载(整数、字符串)调用TreeView.Nodes.Insert 时,您实际上会得到一个TreeNode
返回给您的对象。因此,如果您将样本更改为
Private Sub AddNode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddNode.Click
If Not treeview1.SelectedNode Is Nothing Then
Dim NewNode as TreeNode = treeview1.Nodes.Insert(treeview1.SelectedNode.Index + 1, TextBox1.Text)
treeview.SelectedNode = NewNode
End If
End Sub
then it should select the node you just created.
然后它应该选择您刚刚创建的节点。
Your second example just needs to have one line added to it:
您的第二个示例只需要添加一行:
Private Sub NodeUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NodeUp.Click
Dim CurrentIndex As Integer = treeview1.SelectedNode.Index
Dim CurrentNode As TreeNode = treeview1.SelectedNode
treeview1.SelectedNode.Remove()
treeview1.Nodes.Insert(CurrentIndex - 1, CurrentNode)
treeview1.SelectedNode = CurrentNode
End Sub
This is all working from brain compiler at the moment as I don't have access to Visual Studio to test it, so please let me know if you have any problems.
由于我无法访问 Visual Studio 来测试它,目前这一切都在大脑编译器中工作,所以如果您有任何问题,请告诉我。