-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathToolTipListBox.cs
More file actions
106 lines (90 loc) · 3.54 KB
/
ToolTipListBox.cs
File metadata and controls
106 lines (90 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Windows.Forms;
namespace DigglesModManager
{
/// <summary>
/// Interface for objects used in ToolTipListBox.
/// </summary>
internal interface IToolTipDisplayer
{
string GetToolTipText();
}
/// <summary>
/// ListBox that displays item-specific tooltips.
/// </summary>
internal partial class ToolTipListBox : ListBox
{
// The item index that the mouse is currently over
private int _currentItem;
// A value indicating if the current item has been set
private bool _currentItemSet;
// A value indicating if a tooltip is currently being displayed
private bool _toolTipDisplayed;
// Timer that is used to wait for the mouse to hover over an item
private readonly Timer _toolTipDisplayTimer;
// Tooltip control
private readonly ToolTip _toolTip;
public ToolTipListBox()
{
InitializeComponent();
MouseMove += listBox_MouseMove;
MouseLeave += listBox_MouseLeave;
_currentItemSet = false;
_toolTipDisplayed = false;
_toolTipDisplayTimer = new Timer();
_toolTip = new ToolTip();
// Set the timer interval to the system time that it takes for a tooltip to appear
_toolTipDisplayTimer.Interval = SystemInformation.MouseHoverTime;
_toolTipDisplayTimer.Tick += _toolTipDisplayTimer_Tick;
}
private void listBox_MouseMove(object sender, MouseEventArgs e)
{
// Get the item that the mouse is currently over
var cursorPoint = Cursor.Position;
cursorPoint = PointToClient(cursorPoint);
var itemIndex = IndexFromPoint(cursorPoint);
if (itemIndex == ListBox.NoMatches)
{
// Mouse is over empty space in the listbox so hide tooltip
_toolTip.Hide(this);
_currentItemSet = false;
_toolTipDisplayed = false;
_toolTipDisplayTimer.Stop();
}
else if (!_currentItemSet)
{
// Mouse is over a new item so start timer to display tooltip
_currentItem = itemIndex;
_currentItemSet = true;
_toolTipDisplayTimer.Start();
}
else if (itemIndex != _currentItem)
{
// Mouse is over a different item so hide tooltip and restart timer
_currentItem = itemIndex;
_toolTipDisplayTimer.Stop();
_toolTipDisplayTimer.Start();
_toolTip.Hide(this);
_toolTipDisplayed = false;
}
}
private void listBox_MouseLeave(object sender, EventArgs e)
{
// Mouse has left listbox so stop timer (tooltip is automatically hidden)
_currentItemSet = false;
_toolTipDisplayed = false;
_toolTipDisplayTimer.Stop();
}
void _toolTipDisplayTimer_Tick(object sender, EventArgs e)
{
// Display tooltip text since the mouse has hovered over an item
if (_toolTipDisplayed || _currentItem == ListBox.NoMatches || _currentItem >= Items.Count)
return;
var toolTipDisplayer = Items[_currentItem] as IToolTipDisplayer;
if (toolTipDisplayer == null)
return;
_toolTip.SetToolTip(this, toolTipDisplayer.GetToolTipText());
_toolTipDisplayed = true;
}
}
}