vb.nettreeviewnodestree-nodes

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)

Solution

  • 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