none
How to detect the row of the ListView when move mouse in WinForm RRS feed

  • Question

  • I have a ListView control.

    When I move mouse, program should detect which row the mouse is on.

    How to do this?

    • Moved by CoolDadTx Tuesday, September 17, 2019 2:36 PM Winforms related
    Sunday, September 15, 2019 11:57 PM

Answers

  • Hi Jeff0803,

    Thank you for posting here.

    For your question, you want to detect the row of the ListView when moving mouse in WinForm.

    You could try the following code to do it.


    public partial class Form1 : Form
         {
            public Form1()
            {
                InitializeComponent();
                listView1.ItemMouseHover += listView1_MouseHover;
                listView1.Columns.Add("column1", 75, HorizontalAlignment.Center);
                listView1.Columns.Add("column2", 75, HorizontalAlignment.Center);
                listView1.Columns.Add("column3", 75, HorizontalAlignment.Center);
                label1.Text = "";
            }
    
            private void listView1_MouseHover(object sender, ListViewItemMouseHoverEventArgs e)
            {
                for (int i = 0; i < listView1.Items.Count; i++)
                {
                   ListViewItem item1 = listView1.Items[i];
                    if (e.Item.Equals(item1))
                    {
                        item1.BackColor = Color.LightSkyBlue;
                        String s = "Now is on the row " + i;
                        label1.Text = s;
                    }
                    else {
                        item1.BackColor = Color.White;
                    }
                }
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                string[][] xxx = new string[10][];
                xxx[0] = new string[] { "1", "2", "3", "4" };
                xxx[1] = new string[] { "4", "5", "6" };
                xxx[2] = new string[] { "7", "8", "9" };
                xxx[3] = new string[] { "10", "11", "12" };
                xxx[4] = new string[] { "13", "14", "15" };
                xxx[5] = new string[] { "16", "17", "18" };
                xxx[6] = new string[] { "19", "20", "21" };
                xxx[7] = new string[] { "22", "23", "24" };
                xxx[8] = new string[] { "25", "26", "27" };
                xxx[9] = new string[] { "28", "29", "30" };
                for (int i = 0; i < xxx.Length; i++)
                {
                    ListViewItem item = new ListViewItem(xxx[i]);
                    listView1.Items.Add(item);
                }
                this.listView1.EndUpdate();  
            }
           
        }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Monday, September 16, 2019 5:27 AM
  • Handle the MouseMove event of List View and try a code like this:

    private void ListView1_MouseMove( object sender, MouseEventArgs e )

    {

        var ht = listView1.HitTest( e.Location );

        var item = ht.Item;

        if( item != null )

        {

            . . .

        }

    }

     

    If you need the row number, then use ‘item.Index’.

    • Marked as answer by Jeff0803 Wednesday, September 18, 2019 2:46 PM
    Monday, September 16, 2019 5:37 AM

All replies

  • Hi Jeff0803,

    Thank you for posting here.

    For your question, you want to detect the row of the ListView when moving mouse in WinForm.

    You could try the following code to do it.


    public partial class Form1 : Form
         {
            public Form1()
            {
                InitializeComponent();
                listView1.ItemMouseHover += listView1_MouseHover;
                listView1.Columns.Add("column1", 75, HorizontalAlignment.Center);
                listView1.Columns.Add("column2", 75, HorizontalAlignment.Center);
                listView1.Columns.Add("column3", 75, HorizontalAlignment.Center);
                label1.Text = "";
            }
    
            private void listView1_MouseHover(object sender, ListViewItemMouseHoverEventArgs e)
            {
                for (int i = 0; i < listView1.Items.Count; i++)
                {
                   ListViewItem item1 = listView1.Items[i];
                    if (e.Item.Equals(item1))
                    {
                        item1.BackColor = Color.LightSkyBlue;
                        String s = "Now is on the row " + i;
                        label1.Text = s;
                    }
                    else {
                        item1.BackColor = Color.White;
                    }
                }
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                string[][] xxx = new string[10][];
                xxx[0] = new string[] { "1", "2", "3", "4" };
                xxx[1] = new string[] { "4", "5", "6" };
                xxx[2] = new string[] { "7", "8", "9" };
                xxx[3] = new string[] { "10", "11", "12" };
                xxx[4] = new string[] { "13", "14", "15" };
                xxx[5] = new string[] { "16", "17", "18" };
                xxx[6] = new string[] { "19", "20", "21" };
                xxx[7] = new string[] { "22", "23", "24" };
                xxx[8] = new string[] { "25", "26", "27" };
                xxx[9] = new string[] { "28", "29", "30" };
                for (int i = 0; i < xxx.Length; i++)
                {
                    ListViewItem item = new ListViewItem(xxx[i]);
                    listView1.Items.Add(item);
                }
                this.listView1.EndUpdate();  
            }
           
        }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Monday, September 16, 2019 5:27 AM
  • Handle the MouseMove event of List View and try a code like this:

    private void ListView1_MouseMove( object sender, MouseEventArgs e )

    {

        var ht = listView1.HitTest( e.Location );

        var item = ht.Item;

        if( item != null )

        {

            . . .

        }

    }

     

    If you need the row number, then use ‘item.Index’.

    • Marked as answer by Jeff0803 Wednesday, September 18, 2019 2:46 PM
    Monday, September 16, 2019 5:37 AM