473,385 Members | 1,329 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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...

Dec 22 '06 #1
2 5571
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.lis tViewB_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);

}

}

Dec 22 '06 #2

"Jason Hales" <ja*********@yahoo.comwrote in message
news:11**********************@73g2000cwn.googlegro ups.com...
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.

Dec 22 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Kate | last post by:
Hi: I have a form with a picture box and some command buttons to make certain shapes appear in the picture box. The shapes are drawn on blank UserControls added like this: 'at top of form...
3
by: Christophe Guillon | last post by:
Hello I would like to have a hypertext link which, when I click on it, leads to a web page where a certain word is highlighted. Depending on the link, the same target page would be displayed but...
0
by: Marco Auday | last post by:
I need to associate items in two listviews. I have got the drag-and-drop operation to perform as I wanted. However, I would like the items in the target listview to be highlighted when the mouse...
15
by: 50295 | last post by:
Hi everyone, This one is better experienced than explained, so I'm including a code sample below. Please and save (as an html file) and view with NN or Firefox (or maybe even Mozilla), and then...
3
by: Dan | last post by:
How do I find out what control a DragDrop event comes from? I initially presumed that it was the "sender" parameter. But this always seems to be the destination. Ie ... private void...
9
by: Devron Blatchford | last post by:
Hi there, Just wondering if I change the back and fore colour of a listview item when the mouse hovers over it? I want to overide the default windows colour. Can someone please tell me how to...
3
by: Dean Slindee | last post by:
In a checked listbox, I am allowing drag/drop of the items within (resequencing). Problem is, when dropping a checked item, the checked state always reverts to unchecked (unwanted). Anyone know...
0
by: Gene Hubert | last post by:
Well, it seems fundamental to me anyway. Hopefully it is simple enough. The question is for when the source for the dragdrop is a different application that the target for the dragdrop. How...
2
by: markszlazak | last post by:
In the following script, a control displays (black box) in each table cell once you mouse over the cell. Mouse down on the control to change the mode of the table. Drag the mouse over cells in the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.