none
(TreeView Control) TreeNode / ContextMenu RRS feed

  • Question

  • I have a treeview control, with several treenodes in it. To each treenode I have attached a contextmenu.
    Sofar it works fine: when I click the treenode with the right mouse button, I get the context menu and I can fire off the corresponding menuitem events.

    The question is, how-to properly figure out (within the menuitem event), which treenode was selected, so I can do the selected work on the treenode. It's not the "treeview1.SelectedNode"!

    Thankx, Cheers Harry
    Friday, June 24, 2005 11:18 PM

Answers

  • I suspect you are seeing some behaviour which is there by design.  Right-clicking a node will not select it, so TreeView.SelectedNode may not be the node that was right-clicked.

    Handle the MouseDown event, and select the node at the click location:


    private void treeView1_MouseDown(object sender, MouseEventArgs e)

    {

    if (e.Button == MouseButtons.Right)

    {

    TreeNode clickedNode = treeView1.GetNodeAt(e.Location);

    treeView1.SelectedNode = clickedNode;

    }

    }


     
    Monday, June 27, 2005 5:46 PM

All replies

  • Hi,


    AFAIK, its the SelectedNode property of the treeview that defines the current node selected. What's wrong with it? could you place a code snippet that the problem occurs?





    cheers,


    Paul June A. Domag
    Saturday, June 25, 2005 8:40 AM
  • I suspect you are seeing some behaviour which is there by design.  Right-clicking a node will not select it, so TreeView.SelectedNode may not be the node that was right-clicked.

    Handle the MouseDown event, and select the node at the click location:


    private void treeView1_MouseDown(object sender, MouseEventArgs e)

    {

    if (e.Button == MouseButtons.Right)

    {

    TreeNode clickedNode = treeView1.GetNodeAt(e.Location);

    treeView1.SelectedNode = clickedNode;

    }

    }


     
    Monday, June 27, 2005 5:46 PM
  • Is this the "right" way of doing it?  I'm concerned if there's another way that context menu can be fired without the right mouse button being clicked (can't think of anything off hand...the keyboard key only triggers the selected node's context menu...but who knows what new input devices will do).  I know the SourceControl property of the context menu strip will tell me which control instigated it (the TreeView), but there isn't any property that says which node within it did so.
    Friday, June 16, 2006 12:52 AM