Skip to content

TreeView Nodes Clear #301

Description

@itg-assistant

Alex Prinias asked 2016-10-03 09:35:07 UTC

Hi guys,

I have a treeview, tv,  and a button on  a form. In the button's click event I have the following code:

tv.Nodes.Clear();
var root = new TreeNode();
root.Text = "Root Node";
tv.Nodes.Add(root);

for (int i = 1; i <= 5; i++)
{
var node = new TreeNode();
node.Text = "Node " + i.ToString();
root.Nodes.Add(node);
}

tv.ExpandAll();

I would expect that every time I click the button I get the same treeview, since the first line of my code clears the treeview. However, a new "Root Node" is added every time, and the treeview keeps extending. Am I missing something?

Best,
Alex

Metadata

Metadata

Assignees

No one assigned

    Labels

    GeneralQuestions, how-tos and everything else.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions