Esempio n. 1
0
        // for ControlCollection
        public static bool Swap(this Control.ControlCollection source, int fromIndex, int toIndex)
        {
            Action <int, object> insertAtCallback = (index, item) => {
                source.Add((Control)item);
                source.SetChildIndex((Control)item, index);
            };

            return(source.Swap(fromIndex, toIndex, insertAtCallback));
        }
Esempio n. 2
0
        public static void Shift <T>(this Control.ControlCollection source, int fromIndex, int toIndex, Func <T, bool> isItemMovable, Action <int, int> fromIndex_ToIndexCallback = null, Action <T, T> from_ToCallback = null) where T : Control
        {
            if (isItemMovable((T)source[fromIndex]) && isItemMovable((T)source[toIndex]))
            {
                var ascend = fromIndex < toIndex;

                for (; ;)
                {
                    if (ascend && fromIndex < toIndex || !ascend && fromIndex > toIndex)
                    {
                        var _toIndex = fromIndex;

next:
                        var _fromIndex = fromIndex + (ascend ? 1 : -1);
                        Action next = () => fromIndex = ascend ? fromIndex + 1 : fromIndex - 1;

                        if (_fromIndex == toIndex || _fromIndex != toIndex && isItemMovable((T)source[_fromIndex]))
                        {
                            source.Swap(_fromIndex, _toIndex);
                            fromIndex_ToIndexCallback?.Invoke(_fromIndex, _toIndex);
                            from_ToCallback?.Invoke((T)source[_fromIndex], (T)source[_toIndex]);
                            next();
                        }
                        else
                        {
                            next();
                            goto next;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }