C# - MouseDown event in Tree View Control
Here is a way to get the selected tree view node using C#. The snippet uses the X and Y co-ordinates to get the node
private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
Point p1 = new Point(e.X, e.Y);
TreeNode N1 = treeView1.GetNodeAt(p1) ;
MessageBox.Show(N1.Text);
}
Who ever posted this solution never tried it in a NET application because it doesn't work
ReplyDeleteSame problem
ReplyDeleteThis solution works only on WPC. It doesn't work on a .NET application
Hi,
ReplyDeleteIt does work in .NET as well. What I did was creating a custom treeView, and override the OnMouseDown method:
class CustomTreeView : TreeView
{
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
this.SelectedNode = this.GetNodeAt(e.Location);
}
}
I hope to help!