Search code examples
c#.nettreevieweditchild-nodes

How to edit a label in a new childnode created in a tree view, all of this programatically c# .net


I'm starting to use tree view.

What I'm trying to do is a browse folder form, when the user clicks into a folder, it will display subfolders inside, all of this in a tree view, what I'm trying to do is create a new folder inside of a selected node:

this is an example:

Home ------->Folder1
    |------->Folder2

When click Make new folder button and I have selected "Folder1"

create a new childnode inside of Folder1 and start editing it.

Home -----> Folder1 --->BeginEditNewFolder
    |-----> Folder2

whis is what I have:

TreeNode NodeTocreate = new TreeNode();
NodeTocreate.ImageIndex = 0;
NodeTocreate.Text = string.Empty;
tvRemoteDirectory.SelectedNode.Nodes.Add(NodeTocreate);
// <--- LINE OF CODE HERE TO START EDITING THAT NODE"

and this is What I have in the AfterLabelEdit event to get the name typed:

private void tvRemoteDirectory_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
    MakeDirectory = e.Label;
}

How can I start editing that new folder created?


Solution

  • Make sure your TreeView control has the LabelEdit property set:

    tvRemoteDirectory.LabelEdit = true;
    

    then just call BeginEdit on the SelectedNode:

    NodeTocreate.BeginEdit();