Using the MouseMove event, you can track the index of the element that is above the mouse and store it in a variable that stores its value between MouseMoves. Each time MouseMove starts up, it checks to see if the index has changed. If so, it disables the tooltip, changes the tooltip text for this control, and then reactivates it.
The following is an example where one property of the Car class is displayed in a ListBox, but then full information is displayed when it hangs over any one row. For this example to work, all you need is a ListBox called lstCars, with the MouseMove event and a TextTip component called tt1 on your WinForm.
Vehicle Class Definition:
class Car { // Main properties: public string Model { get; set; } public string Make { get; set; } public int InsuranceGroup { get; set; } public string OwnerName { get; set; } // Read only property combining all the other informaiton: public string Info { get { return string.Format("{0} {1}\nOwner: {2}\nInsurance group: {3}", Make, Model, OwnerName, InsuranceGroup); } } }
Form load event:
private void Form1_Load(object sender, System.EventArgs e) { // Set up a list of cars: List<Car> allCars = new List<Car>(); allCars.Add(new Car { Make = "Toyota", Model = "Yaris", InsuranceGroup = 6, OwnerName = "Joe Bloggs" }); allCars.Add(new Car { Make = "Mercedes", Model = "AMG", InsuranceGroup = 50, OwnerName = "Mr Rich" }); allCars.Add(new Car { Make = "Ford", Model = "Escort", InsuranceGroup = 10, OwnerName = "Fred Normal" }); // Attach the list of cars to the ListBox: lstCars.DataSource = allCars; lstCars.DisplayMember = "Model"; }
Tooltip code (including creating a class level variable called hoveredIndex):
Michael Jul 27 '11 at 17:14 2011-07-27 17:14
source share