How can I highlight the item under the mouse in a dragdrop target?

J

james

Hello

I'm implementing drag and drop between two listviews (in detail view mode).

When I drag an item from box a to box b, I then process information relating
to the two items. The item from box a does not need to be added into box b.

What I can't see how to do is for the item under the mouse in box b when I'm
dragging to get selected/highlighted?
I can get the index of the item in B so I know which item it is, it's just
not very intuitive...
 
J

Jason Hales

James you can use the GetItemAt function to return a ListViewItem under
the mouse, call EnsureVisible so that it auto scrolls thru the items
The following class overrides OnDragOver to do that

public class DragAndDropListView : ListView
{

protected override void OnDragOver(DragEventArgs e)
{
Point cp = base.PointToClient(new Point(e.X, e.Y));
ListViewItem hoverItem = base.GetItemAt(cp.X, cp.Y);
if (hoverItem == null)
{
e.Effect = DragDropEffects.None;
}
else
{
Console.WriteLine(hoverItem.Text);
e.Effect = DragDropEffects.Copy;
hoverItem.Selected = true;
hoverItem.EnsureVisible();
}

base.OnDragOver(e);
}

protected override void OnDragEnter(DragEventArgs e)
{
e.Effect = DragDropEffects.All;
base.OnDragEnter(e);
}
}

Here's a test form :
public class Form1 : Form
{

private System.ComponentModel.IContainer components = null;
private System.Windows.Forms.ListView listViewB;
private DragAndDropListView ListViewA;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.ColumnHeader columnHeader3;
private System.Windows.Forms.ColumnHeader columnHeader4;
private System.Windows.Forms.ColumnHeader columnHeader5;

public Form1()
{
InitializeComponent();

for (int i = 0; i < 50; i ++)
{
ListViewA.Items.Add("List Item " + i);
}
}

private void listViewB_ItemDrag(object sender,
ItemDragEventArgs e)
{
listViewB.DoDragDrop(listViewB.SelectedItems[0],
DragDropEffects.All | DragDropEffects.Link);
}

protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

private void InitializeComponent()
{
System.Windows.Forms.ListViewItem listViewItem1 = new
System.Windows.Forms.ListViewItem("col1");
System.Windows.Forms.ListViewItem listViewItem2 = new
System.Windows.Forms.ListViewItem("col2");
this.listViewB = new System.Windows.Forms.ListView();
this.columnHeader1 = new
System.Windows.Forms.ColumnHeader();
this.columnHeader2 = new
System.Windows.Forms.ColumnHeader();
this.ListViewA = new
How_can_I_highlight_the_item_under_the_mouse_in_a_dragdrop_target.DragAndDropListView();
this.columnHeader3 = new
System.Windows.Forms.ColumnHeader();
this.columnHeader4 = new
System.Windows.Forms.ColumnHeader();
this.columnHeader5 = new
System.Windows.Forms.ColumnHeader();
this.SuspendLayout();
//
// listViewB
//
this.listViewB.AllowDrop = true;
this.listViewB.Columns.AddRange(new
System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2});
this.listViewB.Items.AddRange(new
System.Windows.Forms.ListViewItem[] {
listViewItem1,
listViewItem2});
this.listViewB.Location = new System.Drawing.Point(274,
12);
this.listViewB.Name = "listViewB";
this.listViewB.Size = new System.Drawing.Size(184, 423);
this.listViewB.TabIndex = 1;
this.listViewB.UseCompatibleStateImageBehavior = false;
this.listViewB.View = System.Windows.Forms.View.Details;
this.listViewB.ItemDrag += new
System.Windows.Forms.ItemDragEventHandler(this.listViewB_ItemDrag);
//
// ListViewA
//
this.ListViewA.AllowDrop = true;
this.ListViewA.Columns.AddRange(new
System.Windows.Forms.ColumnHeader[] {
this.columnHeader3,
this.columnHeader4,
this.columnHeader5});
this.ListViewA.FullRowSelect = true;
this.ListViewA.HideSelection = false;
this.ListViewA.Location = new System.Drawing.Point(12, 12);
this.ListViewA.MultiSelect = false;
this.ListViewA.Name = "ListViewA";
this.ListViewA.Size = new System.Drawing.Size(241, 423);
this.ListViewA.TabIndex = 2;
this.ListViewA.UseCompatibleStateImageBehavior = false;
this.ListViewA.View = System.Windows.Forms.View.Details;
//
// columnHeader3
//
this.columnHeader3.Width = 108;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F,
13F);
this.AutoScaleMode =
System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(470, 447);
this.Controls.Add(this.ListViewA);
this.Controls.Add(this.listViewB);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}

}
 
J

james

Jason Hales said:
James you can use the GetItemAt function to return a ListViewItem under
the mouse, call EnsureVisible so that it auto scrolls thru the items
The following class overrides OnDragOver to do that

public class DragAndDropListView : ListView
{

protected override void OnDragOver(DragEventArgs e)
{
Point cp = base.PointToClient(new Point(e.X, e.Y));
ListViewItem hoverItem = base.GetItemAt(cp.X, cp.Y);
if (hoverItem == null)
<snip>

Thanks very much for that - after reading your notes I've managed to
integrate the ideas into my existing project, and it works a treat!
James.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top