[C# Helper]
Index Books FAQ Contact About Rod
[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[Beginning Software Engineering]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Display tooltips for ListView items in C#

[Display tooltips for ListView items in C#]

This example shows how to display different tooltips for the items in a ListBox. When you move the mouse over an item in the ListBox, the program uses the following code to display a tooltip for it.

// Display a tooltip for the animal under the mouse. private void lstWeirdAnimals_MouseMove( object sender, MouseEventArgs e) { // See what item is under the mouse. int index = lstWeirdAnimals.IndexFromPoint(e.Location); // Just use the item's value for the tooltip. string tip = lstWeirdAnimals.Items[index].ToString(); // Display the item's value as a tooltip. if (tipWeirdAnimals.GetToolTip(lstWeirdAnimals) != tip) tipWeirdAnimals.SetToolTip(lstWeirdAnimals, tip); }

When the ListBox receives a MouseMove event, the event handler uses the ListBox control's IndexFromPoint method to see which items is under the mouse. This example just uses the ListBox items' text for the tooltips, but you could modify the program to look up appropriate tooltips for the items, perhaps in a Dictionary.

Next the code uses the tipWeirdAnimals ToolTip component's GetToolTip method to see if the ListBox control's current tooltip is different from the new one. If the two values are different, the code uses the ToolTip component's SetToolTip method to set the new tooltip for the ListBox. (The code doesn't set the tooltip if the new and old values are the same to prevent a really annoying flicker. Comment out the if statement to see it.)

Download the example to experiment with it and to see additional details.

© 2009-2022 Rocky Mountain Computer Consulting, Inc. All rights reserved.