Selecting nodes in a TreeView control with a right button click

Someone asked me yesterday how to select nodes in a System.Windows.Forms.TreeView control, using a right click, instead of a left click.

i.e. this is useful if you are developing a WindowsExplorer-like application, where you will want to select a node with a right click to make a copy-paste operation.

A very easy workaround about the default behavior of the TreeView (which only selects with a left-button click), is something like this:

private void treeView1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y);
}

This event will be fired before the context menu´s Opening event, and will select the node clicked with the right button.

Cheers!