跳到主要內容

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

  • 問題

  • 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

    • 已移動 CoolDadTx 2011年1月17日 下午 02:24 Not IDE related (From:Visual C# IDE)
    2011年1月15日 上午 03:30

解答

  •  

    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.

    2011年1月17日 上午 08:58
    版主

所有回覆

  •  

    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.

    2011年1月17日 上午 08:58
    版主
  •  

    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.

    2011年1月19日 下午 08:26
    版主
  • Hi Mike,

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

    Best regards

    2011年1月20日 下午 05:10
  • Hi Mike,

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

    Best regards

    2011年1月20日 下午 11:10
  • 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.

    2011年1月24日 上午 05:50
    版主