1、绑定move事件
listBox1.MouseMove += new MouseEventHandler(listBox_MouseMove);
2、编写代码
private void listBox_MouseMove(object? sender, MouseEventArgs e)
{
ListBox? listBox = sender as ListBox;
//获取鼠标在ListBox中的位置
int newIndex = listBox.IndexFromPoint(e.X, e.Y);
if (newIndex != ListBox.NoMatches && newIndex != -1)
{
if (oldIndex != newIndex)
{
ControlUtils.ListBoxItemHover(sender,false, oldIndex, newIndex, Color.FromArgb(31, 31, 31), Color.White, Color.FromArgb(55, 55, 61), Color.White);
oldIndex = newIndex;
}
// 设置ToolTip显示的文本
lbToolTip.SetToolTip(listBox, listBox.Items[newIndex].ToString());
}
}