Is it possible to show a hover highlight feature while dragging and dropping in C#?

Spamzilla 41 Reputation points
2023-01-24T01:35:24.2+00:00

My program has this code but it isn't coming together right. I'm wondering if this is even possible.

ListViewItem prevSelected = null;

        private void List_MouseLeave(object sender, EventArgs e)
        {
            if (prevSelected != null)
            {
                prevSelected.BackColor = Color.White;
            }
        }

        private void List_Hover(object sender, ListViewItemMouseHoverEventArgs e)
        {
            if (prevSelected != null)
            {
                prevSelected.BackColor = Color.White;
            }

            ListViewItem tempLVI = e.Item;
            tempLVI.BackColor = Color.FromArgb(0x00, 0xFF, 0x00);

            prevSelected = e.Item;
        }


        private void List_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void List_DragDrop(object sender, DragEventArgs e)
        {
            if (prevSelected != null)
            {
                prevSelected.BackColor = Color.White;
            }
        }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
6,963 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
620 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Spamzilla 41 Reputation points
    2023-01-24T20:35:37.58+00:00

    I found my own solution and it looks like this:

     ListViewItem dragSelected = null;
    
            private void List_DragOver(object sender, DragEventArgs e)
            {
                for (int ii = 0; ii < newListView.Items.Count; ii++)
                {
                    if ((e.Y >= newListView.Items[ii].Position.Y) &&
                        (e.Y <= ((int)(newListView.Items[ii].Position.Y) + (int)(newListView.Items[ii].Bounds.Height) + (int)(this.Location.Y) + 76 ))
                        )
                    {
                        if (dragSelected != null)
                        {
                            dragSelected.BackColor = Color.White;
                        }
    
                        dragSelected = this.newListView.Items[ii];
                        dragSelected.BackColor = Color.FromArgb(0x00, 0xFF, 0x00);
                        break;
    
                    }
                }
    
                e.Effect = DragDropEffects.Copy;
            }
    
    No comments