I have copied a method from on this forum of how to enable items to be dragged up and down a list view. Which works well problem is i also have another function which is called on doubleclick of an item but it seems the mouse dragging events cancel out my double click
Can anyone see where i am going wrong?
Drage Procs...
------------------------------------------------------------------------------------------------
private void listView_MouseDown(object sender, MouseEventArgs e) { // Record the cursor location if (e.Button == MouseButtons.Left) { this.lastMouseDown = e.Location; } else { this.lastMouseDown = null; } } private void listView_MouseMove(object sender, MouseEventArgs e) { UltraListView listView = sender as UltraListView; // If the mouse has moved outside the area in which it was pressed, // start a drag operation if (this.lastMouseDown.HasValue) { Size dragSize = SystemInformation.DragSize; Rectangle dragRect = new Rectangle(this.lastMouseDown.Value, dragSize); dragRect.X -= dragSize.Width / 2; dragRect.Y -= dragSize.Height / 2; if (dragRect.Contains(e.Location)) { UltraListViewItem itemAtPoint = listView.ItemFromPoint(e.Location); if (itemAtPoint != null) { this.lastMouseDown = null; this.dragItem = itemAtPoint; listView.DoDragDrop(this.dragItem, DragDropEffects.Move); } } } } private void listView_QueryContinueDrag(object sender, QueryContinueDragEventArgs e) { UltraListView listView = sender as UltraListView; // Cancel the drag operation if the escape key was pressed if (e.EscapePressed) { this.OnDragEnd(listView, true); e.Action = DragAction.Cancel; } } private void listView_DragOver(object sender, DragEventArgs e) { UltraListView listView = sender as UltraListView; Point clientPos = listView.PointToClient(new Point(e.X, e.Y)); if (this.dragItem != null) { this.dropItem = listView.ItemFromPoint(clientPos); e.Effect = this.dropItem != null && this.dropItem != this.dragItem ? DragDropEffects.Move : DragDropEffects.None; } // TODO: This could be improved with a hover timer // If the cursor is within {dragScrollAreaHeight} pixels // of the top or bottom edges of the control, scroll int dragScrollAreaHeight = 8; Rectangle displayRect = listView.DisplayRectangle; Rectangle topScrollArea = displayRect; topScrollArea.Height = (dragScrollAreaHeight * 2); Rectangle bottomScrollArea = displayRect; bottomScrollArea.Y = bottomScrollArea.Bottom - dragScrollAreaHeight; bottomScrollArea.Height = dragScrollAreaHeight; ISelectionManager selectionManager = listView as ISelectionManager; if (topScrollArea.Contains(clientPos) || bottomScrollArea.Contains(clientPos)) { selectionManager.DoDragScrollVertical(0); } } private void listView_DragDrop(object sender, DragEventArgs e) { UltraListView listView = sender as UltraListView; this.OnDragEnd(listView, false); } private void OnDragEnd(UltraListView listView, bool canceled) { if (canceled == false && this.dragItem != null && this.dropItem != null) { listView.BeginUpdate(); listView.Items.Remove(this.dragItem); int index = this.dropItem.Index + 1; listView.Items.Insert(index, this.dragItem); listView.EndUpdate(); } this.dragItem = this.dropItem = null; this.lastMouseDown = null; }
------------------------------------------------------------------------------
event handlers added in form constructor
---------------------------------------------------------------------------
this.uListSelected.AllowDrop = true;
this.uListSelected.MouseDown += new MouseEventHandler(listView_MouseDown);
this.uListSelected.MouseMove += new MouseEventHandler(listView_MouseMove); this.uListSelected.DragOver += new DragEventHandler(listView_DragOver); this.uListSelected.DragDrop += new DragEventHandler(listView_DragDrop); this.uListSelected.QueryContinueDrag += new QueryContinueDragEventHandler(listView_QueryContinueDrag);
--------------------------------------------------------------------------------
In the MouseMove handler, the following line of code:
if (this.lastMouseDown.HasValue)
Needs to be changed to this:
if (this.lastMouseDown.HasValue && e.Button == MouseButtons.Left )
That seemed to do the trick thanks :)
You are correct; the sample this was taken from has it correct, but that was changed in the code posted here.
I think the line:
if (dragRect.Contains(e.Location))
should actually be:
if (!dragRect.Contains(e.Location))