vb.net 在VB.NET中将子节点添加到特定节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21010174/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:23:01  来源:igfitidea点击:

Adding a child node to specific node in VB.NET

vb.nettreeviewnodestree-nodes

提问by ?a?an ?elik

I have a treeview on my form and in that treeview there are screen resolutions categorized to their type (categories: 16:9, 16:10, 4:3 etc...)and there are one last node which is labelled "Custom".

我的表单上有一个树视图,在该树视图中,屏幕分辨率按其类型分类(类别:16:9、16:10、4:3 等),最后一个节点标记为“自定义” .

I would like to enable users to add their own resolutions by typing numbers in textboxes and clicking a button.

我想让用户通过在文本框中输入数字并单击按钮来添加自己的分辨率。

I have successfully written the code to add nodes but everytime i add a custom resolution, it creates a new root node called "Custom". How can I make them go under one "Custom" node?

我已经成功编写了添加节点的代码,但每次添加自定义分辨率时,它都会创建一个名为“自定义”的新根节点。我怎样才能让它们进入一个“自定义”节点?

Here's my code:

这是我的代码:

Form1.TreeView1.Nodes.Add("Custom").Nodes.Add(TextBox1.Text + "x" + TextBox2.Text)

采纳答案by Fabio

Remove first .Addword in your code:

删除.Add代码中的第一个单词:

Form1.TreeView1.Nodes("Custom").Nodes.Add(TextBox1.Text + "x" + TextBox2.Text)

or make a more safely code

或制作更安全的代码

Dim customnode as TreeNode = Form1.TreeView1.Nodes("Custom")
If customnode IsNot Nothing Then
    customnode.Nodes.Add(TextBox1.Text + "x" + TextBox2.Text)
End If

回答by Gandy

Form1.TreeView1.Nodes.Find("Custom", True).First.Nodes.Add(TextBox1.Text + ":" + TextBox2.Text)

Form1.TreeView1.Nodes.Find("Custom", True).First.Nodes.Add(TextBox1.Text + ":" + TextBox2.Text)

The Find is used to recursively search for the Node with the key "Custom".

Find 用于递归搜索键为“Custom”的节点。