I require the cursor of the form to change to the cursor Cursors.Hand
when the user hovers the pointer over a node with a specifically named parent node.
The issue I am having in implementing this is regarding changing the cursor back to the default one when the user moves the pointer away from the TreeNode
in concern.
I have handled the NodeMouseHover
event of the TreeView
control (as in the code snippet in the end) to change the pointer to the alternative cursor and back to the default cursor when the pointer is moved to another node, but this does not handle the case when the user moves the pointer away from the nodes to a, say, a blank area of the TreeView
control.
My initial, and only, intuition regarding the solution to this problem was to get the location and calculate the area of the TreeNode
s which require the cursor to change and check if the pointer is still on one of these on the event handler of the MouseMove
event of the TreeView
control, but, I believe, this is not an elegant solution as there are a lot of TreeNode
s that require this behavior which would require looping through a lot of them for checking, which in turn may cause the application to be, in rare scenarios, a tad non-responsive.
Thanks in advance.
PS The code snippet in question:
this.treeView.NodeMouseHover += delegate (object sender, TreeNodeMouseHoverEventArgs e)
{
bool isNewCursorAssigned = false;
if (e.Node.Parent != null)
{
if (e.Node.Parent.Text == "someTxt")
{
this.Cursor = Cursors.Hand;
isNewCursorAssigned = true;
}
}
if (isNewCursorAssigned == false && this.Cursor != this.DefaultCursor)
this.Cursor = this.DefaultCursor;
};