PC Review


Reply
Thread Tools Rate Thread

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

 
 
james
Guest
Posts: n/a
 
      22nd Dec 2006
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...

 
Reply With Quote
 
 
 
 
Jason Hales
Guest
Posts: n/a
 
      22nd Dec 2006
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);

}

}

 
Reply With Quote
 
james
Guest
Posts: n/a
 
      22nd Dec 2006

"Jason Hales" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Keyboard and Mouse Events - During DragDrop - Need help =?Utf-8?B?R2FtZXk=?= Microsoft Dot NET 0 21st Aug 2006 04:02 AM
Multiple Item DragDrop w/Listboxes Shmulik Microsoft Dot NET Framework Forms 0 29th Sep 2004 08:02 PM
How to select a target datagrid row in dragdrop operation? =?Utf-8?B?SG93YXJkIEdyZWVu?= Microsoft Dot NET Framework Forms 2 19th Aug 2004 10:40 PM
passing on Control DragDrop to parent DragDrop Microsoft C# .NET 0 12th Dec 2003 08:24 AM
Detecting DragDrop target when dropped into Explorer Simon Bond Microsoft Dot NET Framework Forms 0 21st Aug 2003 02:29 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:38 PM.