Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Menu Game CSM - YouTube[^]

How do I show a rectangle around listviewItem when I hover the mouse over it like in the video? I use mousehover / mousemove event. I need a sample code (C# - WinForm) that can do that. Sorry I use google translate


What I have tried:

my listview item have image and text.
How to show image and text in DrawItem

		private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
          
            Color textColor = SystemColors.WindowText;
            if (e.Item.Selected)
            {
                if (listView1.Focused)
                {
                    textColor = SystemColors.HighlightText;
                    e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds);
                }
                else if (!listView1.HideSelection)
                {
                    textColor = SystemColors.ControlText;
                    e.Graphics.FillRectangle(SystemBrushes.Control, e.Bounds);
                }
            }
            else
            {
                using (SolidBrush br = new SolidBrush(listView1.BackColor))
                {
                    e.Graphics.FillRectangle(br, e.Bounds);
                }
            }
            e.Graphics.DrawRectangle(Pens.Red, e.Bounds);
            TextRenderer.DrawText(e.Graphics, e.Item.Text, listView1.Font, e.Bounds,
                                  textColor, Color.Empty,
                                  TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
        }
Posted
Updated 26-Feb-20 20:19pm
v2

1 solution

I'd use Control.MouseMove Event (System.Windows.Forms) | Microsoft Docs[^] or Control.MouseMove Event (System.Windows.Forms) | Microsoft Docs[^] of ListView control[^].
Then...
C#
 ListViewHitTestInfo hti = listView1.HitTest(e.X, e.Y); //where e is System.Windows.Forms.MouseEventArgs
 if(hti.Item == null) return;
//get index of subitem:
 var subid = hti.Item.SubItems.IndexOf(ht.SubItem);
//further instructions here....


Please, take a look at this CP article: ListView with Item-Hover Event[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900