Home > Software design >  Nothing happens when I RIGHT click the Tree Node on the TreeView (Windows Form)
Nothing happens when I RIGHT click the Tree Node on the TreeView (Windows Form)

Time:04-29

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

CodePudding user response:

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