Search code examples
c#winformstreenode

Nothing happens when I RIGHT click the Tree Node on the TreeView (Windows Form)


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");
    }
}

Solution

  • 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();
        }
    }