Reputation: 15
Basically I have a treeview populated with numerous image files. I am trying to make the NodeMouseHover event bring up a little preview of the image. To do this I need to find out what node the mouse is over, but I cannot get it to work, it is unable to find the tree node at the cursor position.
Here is a simplified version my code
private void TreeBroswer_NodeMouseHover(object sender, TreeNodeMouseHoverEventArgs e)
{
string filePath;
PictureBox preview;
TreeNode test = TreeBroswer.GetNodeAt(Cursor.Position.X, Cursor.Position.Y);
//Also tried MousePosition.X,MousePosition.Y
if (test == null)
{
MessageBox.Show("No tree node");
}
else
{
filePath = test.FullPath;
preview = new PictureBox();
preview.ImageLocation = @filePath;
// Display preview
}
}
It fails to get the tree node no matter where my mouse is. I am not sure if I am getting my mouse position wrong or i'm using GetNodeAt wrong, or both.
Upvotes: 0
Views: 4808
Reputation: 66509
The parameter for that event - TreeNodeMouseHoverEventArgs
- already has the information you need.
Just reference e.Node
to see which node the mouse is currently hovering over. If you're not hovering over a node, the event won't fire, so no need to check for null.
private void TreeBroswer_NodeMouseHover(object sender, TreeNodeMouseHoverEventArgs e)
{
var preview = new PictureBox { ImageLocation = e.Node.FullPath };
// Display preview
}
Upvotes: 4
Reputation: 5233
The problem is at the arguments you use in the
TreeBrowser.GetNodeAt(Cursor.Position.X, Cursor.Position.Y)
Try Change to
treeView1.PointToClient(Cursor.Position)
Or using the arguments of TreeNodeMouseHoverEventArgs
this.treeView1.GetNodeAt(e.X, e.Y);
Upvotes: 2