DotNet Developer
DotNet Developer

Reputation: 3018

.Net WinForms. How is selection rectangle of ListView controlled programmatically?

I was doing an experiment with ListView and faced an issue with its selection rectangle. I created a windows forms application, put 2 ListView controls (listView1, listView2) onto Form1.

I set properties of the controls as below at Design-Time:

listView1: Added 3 items ("Item1", "Item2", "Item3")

listView2: AllowDrop = true.

enter image description here

I wrote code to drag-drop items from listView1 to listView2 as follows.

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void listView1_MouseDown(object sender, MouseEventArgs e)
        {
            ListView listView = ((ListView)sender);
            ListViewItem listViewItem = listView.GetItemAt(e.X, e.Y);
            if (listViewItem != null)
            {
                listView.DoDragDrop(listViewItem, DragDropEffects.Move);
            }
        }

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

        private void listView2_DragDrop(object sender, DragEventArgs e)
        {
            ListView listView = ((ListView)sender);
            ListViewItem listViewItem = ((ListViewItem)e.Data.GetData(typeof(ListViewItem)));
            listView1.Items.Remove(listViewItem);
            listView.Items.Add(listViewItem);
        }
    }
}

Now I am running application and drag-dropping items from listView1 to listView2.

enter image description here

enter image description here

enter image description here

enter image description here

Here I am releasing mouse left button. The item from listView1 is successfully getting exported to destination but in listView1 the selection rectangle is getting drawn.

enter image description here

I am not pressing any other key after mouse left button is released, that is all the buttons are in UP-state but the listView1 is happily reacting to mouse move event and changing selection.

enter image description here enter image description here

I couldn't find a way to disable the selection rectangle. I understand that its system doing all this. And it seems to me that there is no way left for .Net developer to be able to do anything about that. So I decided to ask:

How does a .Net developer control selection rectangle of ListView programmatically?

Upvotes: 1

Views: 488

Answers (1)

Idle_Mind
Idle_Mind

Reputation: 39152

Instead of MouseDown(), initiate the drag in MouseMove() like this:

private void listView1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        ListView listView = ((ListView)sender);
        ListViewItem listViewItem = listView.GetItemAt(e.X, e.Y);
        if (listViewItem != null)
        {
            listView.DoDragDrop(listViewItem, DragDropEffects.Move);
        }
    }
}

Upvotes: 1

Related Questions