This article discusses how the ToolTipsFactory tooltips can be used with the ListView-control provided by the .NET-Framework.
The ListView-control is often used to display a list of items with item text and, optionally, an icon to identify the type of item. In contrast to ListBox-controls, it allows to display the items in four different views (please see the documentation about the ListView-control for more details):
Large Icons View Small Icons View List View Details View
Of course, the ListView-control is also a nice control, to be enhanced with ToolTipsFactory tooltips. For instance, the tooltips could be used to display information from the subitems attached to a ListViewItem, which are only visible if the details view is selected, or, if a ListViewItem is just a reference to a more complex object, the tooltips could provide the means to present more information about this specific object to the user in a really natural way.
The approach, how the ToolTipsFactory tooltips can be used to dynamically display information for specific ListViewItems in a ListView-control, does only differ in little details from the approach described for the Listbox-control (see the chapter on "How to use ToolTips with the ListBox-control").
To not be repetitive, we will only focus on the differences.
Exactly as for the Listbox-control, it is possible to use the ToolTipsFactory tooltips in two different ways. One is to let the tooltip only display additional information about the selected ListViewItem, while the other is to display additional information about the ListViewItem over which the mouse is hovering.
The first way, to display a tooltip for the selected ListViewItem is identical to the code discussed in the chapter on "How to use ToolTips with the ListBox-control": The tooltip content is changed in the event-handler for the SelectedIndexChanged-event.
The dynamical approach, to change the tooltip-content as soon as the mouse hovers over a new ListViewItem, only needs small modifications to make the code from the ListBox-control sample work for the ListView-control as well.
Private Sub lvCustomers2_MouseMove(ByVal sender As Object, _
ByVal e As MouseEventArgs) _
Handles lvCustomers2.MouseMoveEnd Sub
'We need this in order to detect the crossing'from one ListViewItem to another... Static Dim oldItem As ListViewItem = Nothing
'Here we get the ListViewItem for'the current mouse-coordinates... Dim newItem As ListViewItem = lvCustomers2.GetItemAt(e.X, e.Y)
'If the mouse does not point to a ListViewItem, 'an already visible tooltip should be switched off. '
If newItem Is Nothing Then
oldItem = newItem
'If the entry, to which the mouse points'did not change, there is no need to change the text 'of the tooltip. If newItem.Index = oldItem.Index Then Exit Sub
'Get the Customer-data related to newItem...Dim c As Customer
c = GetCustomer(newItem.Text)
'With the values provided by the Customer-object, we can 'build the (multiline) string to be displayed by the 'tooltip.
newText &= "ID : " & c.CustomerID.ToString & vbNewLine
newText &= "Customer : " & c.LastName & "," & c.Name & vbNewLine
newText &= "City : " & c.City & vbNewLine
newText &= "Birthdate : " & c.Birthdate.ToShortDateString & vbNewLine
newText &= "Orders : " & c.PendingOrders.ToString
'the new text. Me.MultiLineTT.GetMultiLineToolTip(lbCustomers2).Text = newText
'This is the crucial line of code, which makes the tooltip display
'newItem is the ListViewItem for which the tooltip currently 'displays additional information. Therefore, we set oldItem 'to be the current ListViewItem (newItem).
oldItem = newItem
'If the AutoPopDelay of MuliLineTT has elapsed (Active=False), 'the ToolTip will not be displaying anymore. If we want to 'display the additional information for the new (current) ListViewItem, 'it must be restarted... If Me.MultiLineTT.Active = False Then Me.MultiLineTT.Start(Me.lvCustomers2) End If
A complete sample project, which integrates all four ToolTipsFactory tooltips with the ListView-control, is discussed in another section of this documentation ("TipEx - A tooltips based Windows Explorer"). The commented source-code and the corresponding Visual Studio project for this sample can be found in the "<ToolTipsFactoryHomeDir>.\Samples\TipEx"-directory.