user1402902
user1402902

Reputation:

Adding a child node to specific node in VB.NET

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)

Upvotes: 1

Views: 8074

Answers (2)

Fabio
Fabio

Reputation: 32463

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

Upvotes: 1

Gandy
Gandy

Reputation: 11

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

Upvotes: 0

Related Questions