none
TreeView Contro; in C#: How to highlight a selected node by Mouse-Right-Click ? RRS feed

  • Question

  • In a TreeView control, a normal mouse-left-click highlights the selected node. However, a mouse-right-click does NOT. How can a mouse-right-click highlight the specified node in C# ?

    Thanks

    • Moved by CoolDadTx Monday, January 17, 2011 2:24 PM Not IDE related (From:Visual C# IDE)
    Saturday, January 15, 2011 3:30 AM

Answers

  •  

    Hi  TFukuda-DocumentFusion,

     

    We just need to execute the following code when we use the mouse right button click on the node:

    // Sets the tree node that is currently selected in the tree view control.
    treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y);  
    

    And the complete method is:

    private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
     if (e.Button == System.Windows.Forms.MouseButtons.Right)
     {
     treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y); 
     }
    }
    

    If there's any concern, please feel free to follow up.

     

    Have a nice day!


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, January 17, 2011 8:58 AM
    Moderator

All replies

  •  

    Hi  TFukuda-DocumentFusion,

     

    We just need to execute the following code when we use the mouse right button click on the node:

    // Sets the tree node that is currently selected in the tree view control.
    treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y);  
    

    And the complete method is:

    private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
     if (e.Button == System.Windows.Forms.MouseButtons.Right)
     {
     treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y); 
     }
    }
    

    If there's any concern, please feel free to follow up.

     

    Have a nice day!


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, January 17, 2011 8:58 AM
    Moderator
  •  

    Hi TFukuda-DocumentFusion,

     

    Have you tried the suggestion? Has this question been solved?

     

    If there's any concern, please feel free to let us know.

     

    Have a nice day!


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, January 19, 2011 8:26 PM
    Moderator
  • Hi Mike,

    Sorry, I was out of town. I am going to try the way you discribed, and will update the status.

    Best regards

    Thursday, January 20, 2011 5:10 PM
  • Hi Mike,

    Your suggestion works fine. Thank you very much for your support.

    Best regards

    Thursday, January 20, 2011 11:10 PM
  • Hi TFukuda-DocumentFusion,

     

    You're welcome!

     

    If there's any concern, please feel free to let me know.

     

    Have a nice day!


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, January 24, 2011 5:50 AM
    Moderator