-1

I am trying to create a new node when I right click on the treenode.

private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        treeView1.Nodes[0].Nodes.Add("Folder");
    }
}
Filburt
  • 17,626
  • 12
  • 64
  • 115
Noob69
  • 1
  • 1
  • Did you try setting a breakpoint inside your event handler to see if it enters? Is your event handler registered to the treeView1 control? – Filburt Apr 29 '22 at 12:48

1 Answers1

0

It's working for me. Check if the event is associated. Also, if you want add a child node, use SelectedNode.

private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
    if (e.Button == MouseButtons.Right && treeView1.SelectedNode != null)
    {
        treeView1.SelectedNode.Nodes.Add("Folder");
            
        // To make visible the inserted node
        treeView1.SelectedNode.Expand();
    }
}
Victor
  • 2,313
  • 2
  • 5
  • 13
  • 2
    Maybe you want to add a Node to `e.Node.Nodes` instead? `SelectedNode` can be something different than the Node where the Mouse Click event is generated (a right-click doesn't move the selection). – Jimi Apr 29 '22 at 13:09