Exemple #1
0
        /// <summary>
        /// Raises the <see cref="ItemDrag" /> event.
        /// </summary>
        /// <param name="e">The <see cref="ListBoxItemDragEventArgs" /> instance containing the event data.</param>
        protected virtual void OnItemDrag(ListBoxItemDragEventArgs e)
        {
            EventHandler <ListBoxItemDragEventArgs> handler;

            handler = this.ItemDrag;

            handler?.Invoke(this, e);
        }
Exemple #2
0
        /// <summary>
        /// Raises the <see cref="E:System.Windows.Forms.Control.DragDrop"/> event.
        /// </summary>
        /// <param name="drgevent">A <see cref="T:System.Windows.Forms.DragEventArgs"/> that contains the event data. </param>
        protected override void OnDragDrop(DragEventArgs drgevent)
        {
            if (this.IsDragging)
            {
                try
                {
                    InsertionIndex = CheckInsertionIndex(InsertionIndex);

                    if (this.InsertionIndex != InvalidIndex)
                    {
                        int dragIndex;
                        int dropIndex;

                        dragIndex = (int)drgevent.Data.GetData(typeof(int));
                        dropIndex = this.InsertionIndex;

                        if (dragIndex < dropIndex)
                        {
                            dropIndex--;
                        }
                        if (this.InsertionMode == InsertionMode.After && dragIndex < this.Items.Count - 1)
                        {
                            dropIndex++;
                        }

                        if (dropIndex != dragIndex)
                        {
                            ListBoxItemDragEventArgs args;
                            Point clientPoint;

                            clientPoint = this.PointToClient(new Point(drgevent.X, drgevent.Y));
                            args        = new ListBoxItemDragEventArgs(dragIndex, dropIndex, this.InsertionMode, clientPoint.X, clientPoint.Y);

                            this.OnItemDrag(args);

                            if (!args.Cancel)
                            {
                                object dragItem;
                                try
                                {
                                    dragItem = this.Items[dragIndex];

                                    this.Items.Remove(dragItem);
                                    this.Items.Insert(dropIndex, dragItem);
                                    this.SelectedItem = dragItem;
                                }
                                catch (Exception) { }
                            }
                        }
                    }
                }
                finally
                {
                    //this.Invalidate(this.InsertionIndex);
                    Invalidate();
                    this.InsertionIndex = InvalidIndex;
                    this.InsertionMode  = InsertionMode.None;
                    this.IsDragging     = false;
                }
            }
            Invalidate();
            base.OnDragDrop(drgevent);
        }