I want to add a ToolTip to TreeView nodes and make a balloon appear at the mouse coordinates:
private void treeView1_MouseMove(object sender, MouseEventArgs e)
{
node = treeView1.getNodeAt(e.X, e.Y);
toolTip1 = new ToolTip();
toolTip1.IsBalloon = true;
toolTip1.InitialDelay = 500;
toolTip1.SetToolTip(treeView1, node.Text);
}
The problem is that the ToolTip is assigned to the TreeView, not to the TreeNode, and the position of the balloon is quite strange – not at the expected mouse coordinates, but somewhere in treeView1.
I was trying to use toolTip1.Show() but the InitialDelay property is ignored.
How can I position a balloon ToolTip over a TreeNode and delay its display?
The
ToolTipcontrol is not necessary in order to display tooltips for theTreeNodes. TheTreeViewhas a propertyShowNodeToolTipsthat you can set totrueand theTreeNodeshave aToolTipTextproperty.However, if you want to display the
ToolTipas a balloon, things get more complicated. Fortunately theTreeViewhas aNodeMouseHoverevent. Together with aTimer, we can make ourToolTipbehave as expected.In our form, we make these declarations and set the timer event handler
In
NodeMouseHoverwe initiate the processThe timer will be started twice: once for the initial delay and once for maximum display time of the balloon. Therefore we must handle these two cases in the
timer.Tickevent handlerFinally, we do not want to display the
ToolTipif the mouse leaves theTreeView