How to change the background color of a ListBox Item when hovering?

josh

How do I change the Background Color of a ListBox Item when I hover over it?
I have overridden the DrawItem Event with this code:

private void DrawListBox(object sender, DrawItemEventArgs e)
{
    e.DrawBackground();         
    Graphics g = e.Graphics;
    Brush brush = ((e.State & DrawItemState.Selected) == DrawItemState.Selected) ?
        new SolidBrush(Color.FromArgb(52, 146, 204)) : new SolidBrush(e.BackColor);
    g.FillRectangle(brush, e.Bounds);

    e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font,
         new SolidBrush(e.ForeColor), e.Bounds, StringFormat.GenericDefault);
    e.DrawFocusRectangle();
}

However, a ListBox does not have a Property which contains the hovered Item. There is also no Event like MouseEnterItem, MouseHoverItem Event, or anything similar on a ListBox to subscribe to.

I did quite a lot of research, but all I found was a similar question on SO, which confused ListBox and ListView: Hightlight Listbox item on mouse over event

SQLDiver

As the ListBox does not provide the MouseEnterItem and MouseHoverItem Events, it is necessary to code this functionality yourself, tracking the coordinates of the mouse to determine which item the mouse is over.

The following question is very similar, aimed at showing a tooltip for each item when hovered over. Michael Lang's answer is a very good workaround and should be adaptable for your purposes:

How can I set different Tooltip text for each item in a listbox?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to change the background color of a ListBox Item when hovering?

From Dev

How to change text color (and background) on nav bar links when hovering?

From Dev

How to change text color (and background) on nav bar links when hovering?

From Dev

Change background color of row when hovering

From Dev

Change background color of selected item in listbox

From Dev

Change ListBox Item Background color programmatically

From Dev

Change color of ListBox Item when Clicked on this Item

From Dev

how to change the background of the selected item in a listbox?

From Dev

how to change the background of the selected item in a listbox?

From Dev

Change background colour of menu link when hovering over menu item

From Dev

Change BODY background color when hovering over a table cell

From Dev

How to change background color of an item when the user clicks on

From Dev

Change combobox item back color when hovering over it

From Dev

change ListBox's particular Item background color at bind time

From Dev

Background changes color when hovering

From Dev

How to Change background Color while Hovering in Eclipse Editor

From Dev

How to change Listbox item color from selected Item(object)?

From Dev

Delphi change item background in listbox when double clicked

From Dev

How to change the background color of qtooltip of a qtablewidget item?

From Dev

How to Change background color of list item in Android?

From Dev

Highcharts: how to change line color when hovering a scatterplot series

From Dev

How to change color on every element in div when hovering?

From Dev

how to make color change to white when hovering over button

From Dev

How to change color of text when hovering on anything on a card?

From Dev

how to change listbox item color c# based on its index

From Dev

Changing background image when hovering on a list item

From Dev

how to set a listbox background color

From Dev

gmaps4rails change marker color when hovering over item in sidebar

From Dev

gmaps4rails change marker color when hovering over item in sidebar

Related Related

  1. 1

    How to change the background color of a ListBox Item when hovering?

  2. 2

    How to change text color (and background) on nav bar links when hovering?

  3. 3

    How to change text color (and background) on nav bar links when hovering?

  4. 4

    Change background color of row when hovering

  5. 5

    Change background color of selected item in listbox

  6. 6

    Change ListBox Item Background color programmatically

  7. 7

    Change color of ListBox Item when Clicked on this Item

  8. 8

    how to change the background of the selected item in a listbox?

  9. 9

    how to change the background of the selected item in a listbox?

  10. 10

    Change background colour of menu link when hovering over menu item

  11. 11

    Change BODY background color when hovering over a table cell

  12. 12

    How to change background color of an item when the user clicks on

  13. 13

    Change combobox item back color when hovering over it

  14. 14

    change ListBox's particular Item background color at bind time

  15. 15

    Background changes color when hovering

  16. 16

    How to Change background Color while Hovering in Eclipse Editor

  17. 17

    How to change Listbox item color from selected Item(object)?

  18. 18

    Delphi change item background in listbox when double clicked

  19. 19

    How to change the background color of qtooltip of a qtablewidget item?

  20. 20

    How to Change background color of list item in Android?

  21. 21

    Highcharts: how to change line color when hovering a scatterplot series

  22. 22

    How to change color on every element in div when hovering?

  23. 23

    how to make color change to white when hovering over button

  24. 24

    How to change color of text when hovering on anything on a card?

  25. 25

    how to change listbox item color c# based on its index

  26. 26

    Changing background image when hovering on a list item

  27. 27

    how to set a listbox background color

  28. 28

    gmaps4rails change marker color when hovering over item in sidebar

  29. 29

    gmaps4rails change marker color when hovering over item in sidebar

HotTag

Archive