Nó đã cho tôi chỉ có hai năm để tìm ra câu trả lời cho bạn, nhưng ở đây nó là:
Các DrawItemState.HotLight chỉ áp dụng cho chủ sở hữu rút ra menu, không ListBox. Đối với ListBox, bạn phải theo dõi mục đó:
public partial class Form1 : Form
{
private int _MouseIndex = -1;
public Form1()
{ InitializeComponent(); }
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
Brush textBrush = SystemBrushes.WindowText;
if (e.Index > -1)
{
if (e.Index == _MouseIndex)
{
e.Graphics.FillRectangle(SystemBrushes.HotTrack, e.Bounds);
textBrush = SystemBrushes.HighlightText;
}
else
{
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds);
textBrush = SystemBrushes.HighlightText;
}
else
e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
}
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, textBrush, e.Bounds.Left + 2, e.Bounds.Top);
}
}
private void listBox1_MouseMove(object sender, MouseEventArgs e)
{
int index = listBox1.IndexFromPoint(e.Location);
if (index != _MouseIndex)
{
_MouseIndex = index;
listBox1.Invalidate();
}
}
private void listBox1_MouseLeave(object sender, EventArgs e)
{
if (_MouseIndex > -1)
{
_MouseIndex = -1;
listBox1.Invalidate();
}
}
}
Vâng, 2 năm sau, bạn cũng đã giúp tôi! ;) Nice, cách đơn giản để 'theo dõi nóng' trong một ListBox ... cảm ơn! – ChandlerPelhams
Và LarsTech sẽ giúp nhiều người trong nhiều năm tới ... Tôi đang tìm kiếm một câu trả lời tương tự và tìm thấy nó ở đây. Cảm ơn. – ThN
Làm thế nào điều này có thể được thực hiện cho một combobox? Combobox không có IndexFromPoint. –