Esempio n. 1
0
        //
        // Item Drag event handler
        //
        protected override void OnItemDrag(ItemDragEventArgs e)
        {
            base.OnItemDrag(e);

            base.OnItemDrag(e);

            _draggedItems = new List <ListViewItem>();

            foreach (ListViewItem item in SelectedItems)
            {
                _draggedItems.Add(item);
            }

            ListViewItemDragEventArgs evArgs = new ListViewItemDragEventArgs(ListViewItemDragEventType.DragStart, ListViewItemDragEventBehavior.PlaceBeforeOrAfterAuto, _draggedItems, null);

            if (DragOperation != null)
            {
                DragOperation(evArgs);

                // Cancel the operation if the user specified so
                if (evArgs.Cancel)
                {
                    return;
                }
            }

            DoDragDrop(_draggedItems, DragDropEffects.Move);

            InsertionMark.Index = 0;
        }
Esempio n. 2
0
        //
        // OnDragOver event handler. Updates the dragged node's ghost image's position
        //
        protected override void OnDragOver(DragEventArgs drgevent)
        {
            base.OnDragOver(drgevent);

            // Cancel if no node is being dragged
            if (_draggedItems == null)
            {
                return;
            }

            // Get actual drop item
            Point controlP = PointToClient(new Point(drgevent.X, drgevent.Y));
            int   index    = InsertionMark.NearestIndex(controlP);

            if (index == -1)
            {
                return;
            }

            InsertionMark.Index = index;

            ListViewItem dropItem = Items[index];//GetItemAt(controlP.X, controlP.Y);

            if (dropItem != _tempDropItem)
            {
                ListViewItemDragEventArgs evArgs = new ListViewItemDragEventArgs(ListViewItemDragEventType.DragOver, ListViewItemDragEventBehavior.PlaceBeforeOrAfterAuto, _draggedItems, dropItem);

                if (DragOperation != null)
                {
                    DragOperation(evArgs);

                    // Cancel the operation if the user specified so
                    if (evArgs.Cancel)
                    {
                        drgevent.Effect = DragDropEffects.None;
                        OnDragDrop(drgevent);
                        return;
                    }
                    if (!evArgs.Allow)
                    {
                        return;
                    }
                }

                drgevent.Effect = DragDropEffects.Move;

                // Dissalow the drag here
                if (!evArgs.Allow)
                {
                    drgevent.Effect = DragDropEffects.None;
                }

                _tempDropItem = dropItem;
            }
        }
Esempio n. 3
0
        //
        // OnDragDrop event handler. Ends a node dragging
        //
        protected override void OnDragDrop(DragEventArgs drgevent)
        {
            base.OnDragDrop(drgevent);

            // Cancel if no node is being dragged
            if (_draggedItems == null)
            {
                _timer.Start();

                return;
            }

            if (drgevent.Effect == DragDropEffects.None)
            {
                // Set drag node and temp drop node to null
                _draggedItems = null;
                _tempDropItem = null;

                // Disable scroll timer
                _timer.Enabled = false;

                _timer.Start();

                return;
            }

            if (InsertionMark.Index == -1)
            {
                _timer.Start();

                return;
            }

            // Get drop item
            ListViewItem dropItem = Items[InsertionMark.Index];

            // Launch the feedback for the drag operation
            ListViewItemDragEventArgs evArgs = new ListViewItemDragEventArgs(ListViewItemDragEventType.DragEnd, ListViewItemDragEventBehavior.PlaceBeforeOrAfterAuto, _draggedItems, dropItem);

            if (DragOperation != null)
            {
                DragOperation(evArgs);

                // Cancel the operation if the user specified so
                if (evArgs.Cancel)
                {
                    _timer.Start();

                    return;
                }
            }

            // If drop node isn't equal to drag node, add drag node as child of drop node
            if (_draggedItems[0] != dropItem)
            {
                int index = InsertionMark.Index;

                SelectedItems.Clear();

                foreach (ListViewItem item in _draggedItems)
                {
                    Items.Remove(item);
                    Items.Add(item);

                    item.Selected = true;
                }

                // Deal with a bug from the framework that adds all items to the end even though you insert
                // them at other indexes by also pushing all items after the current selection to the end

                for (int i = index; i < Items.Count - SelectedItems.Count; i++)
                {
                    ListViewItem item = Items[index];

                    Items.Remove(item);
                    Items.Add(item);
                }

                // Launch the feedback for the drag operation
                evArgs = new ListViewItemDragEventArgs(ListViewItemDragEventType.AfterDragEnd, evArgs.EventBehavior, _draggedItems, dropItem);

                DragOperation?.Invoke(evArgs);

                // Set drag node and temp drop node to null
                _draggedItems = null;
                _tempDropItem = null;

                // Disable scroll timer
                _timer.Enabled = false;
            }

            _timer.Start();
        }