のToolTipツールチップを表示するためにコントロールは必要ありませんTreeNodes。には設定できるTreeViewプロパティがあり、にはプロパティがあります。ShowNodeToolTipstrueTreeNodesToolTipText
ただし、 をToolTip吹き出しとして表示する場合は、さらに複雑になります。幸いなことTreeViewに、NodeMouseHoverイベントがあります。とともに、期待どおりに動作さTimerせることができます。ToolTip
このフォームでは、これらの宣言を行い、タイマー イベント ハンドラーを設定します。
private const int InitialToolTipDelay = 500, MaxToolTipDisplayTime = 2000;
private ToolTip toolTip = new ToolTip();
private Timer timer = new Timer();
private TreeNode toolTipNode;
public frmTreeViewWithToolTip()
{
InitializeComponent();
toolTip.IsBalloon = true;
timer.Tick += new EventHandler(timer_Tick);
}
プロセスNodeMouseHoverを開始します
private void treeView_NodeMouseHover(object sender,
TreeNodeMouseHoverEventArgs e)
{
timer.Stop();
toolTip.Hide(this);
toolTipNode = e.Node;
timer.Interval = InitialToolTipDelay;
timer.Start();
}
タイマーは 2 回開始されます。1 回は初期遅延用、もう 1 回はバルーンの最大表示時間用です。したがって、timer.Tickイベント ハンドラでこれら 2 つのケースを処理する必要があります。
void timer_Tick(object sender, EventArgs e)
{
timer.Stop();
if (timer.Interval == InitialToolTipDelay) {
Point mousePos = treeView.PointToClient(MousePosition);
// Show the ToolTip if the mouse is still over the same node.
if (toolTipNode.Bounds.Contains(mousePos)) {
// Node location in treeView coordinates.
Point loc = toolTipNode.Bounds.Location;
// Node location in form client coordinates.
loc.Offset(treeView.Location);
// Make balloon point to upper right corner of the node.
loc.Offset(toolTipNode.Bounds.Width - 7, -12);
toolTip.Show("Node: " + toolTipNode.Text, this, loc);
timer.Interval = MaxToolTipDisplayTime;
timer.Start();
}
} else {
// Maximium ToolTip display time exceeded.
toolTip.Hide(this);
}
}
最後にToolTip、マウスがTreeView
private void treeView_MouseLeave(object sender, EventArgs e)
{
timer.Stop();
toolTip.Hide(this);
}