Adding a child node to specific node in VB.NET

7.9k views Asked by At

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".

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)
2

There are 2 answers

1
Fabio On BEST ANSWER

Remove first .Add word in your code:

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
0
Gandy On

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".