Example #1
0
        /// <summary>アイテムを上下に一つ移動</summary>
        public bool bxMoveItems(ListBox box, int direction, IList boxItemsSource = null)
        {
            try
            {
                if (box == null || box.SelectedIndex < 0) return false;

                var boxItems = boxItemsSource ?? box.Items;
                var selected = box.SelectedItemsList();//連続移動の視点固定のため順番保持
                int iCount = boxItems.Count;//固定
                var r = direction >= 0 ? (Func<int, int>)(i => iCount - 1 - i) : (i => i);

                for (int i = 0; i < boxItems.Count; i++)
                {
                    var item = boxItems[r(i)];
                    if (box.SelectedItems.Contains(item) == true)
                    {
                        boxItems.RemoveAt(r(i));
                        boxItems.Insert(r((i + iCount - 1) % iCount), item);
                        if (i == 0) break;
                    }
                }

                box.UnselectAll();
                TargetBoxItemsRefresh(box, boxItemsSource);
                box.SelectedItemsAdd(selected);
                box.ScrollIntoView(direction < 0 ? selected[0] : selected.Last());
            }
            catch (Exception ex) { MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace); }
            return true;
        }
Example #2
0
        /// <summary>アイテムをボックス内にドロップ</summary>
        public bool bxMoveItemsDrop(ListBox box, object dropTo, IList boxItemsSource = null)
        {
            try
            {
                if (box == null || box.SelectedIndex < 0) return false;

                var boxItems = boxItemsSource ?? box.Items;

                //選択の上と下でドロップ位置を調整する。
                int idx_dropTo = boxItems.IndexOf(dropTo);
                idx_dropTo = idx_dropTo < 0 ? boxItems.Count : idx_dropTo;
                idx_dropTo += (idx_dropTo >= box.SelectedIndex ? 1 : 0);

                var selected = box.SelectedItemsList(true);

                var insertItem = boxItems.Cast<object>().Skip(idx_dropTo).FirstOrDefault(item => !selected.Contains(item));
                boxItems.RemoveItemsAx(selected);//削除はこのタイミング
                int insertIdx = insertItem == null ? boxItems.Count : boxItems.IndexOf(insertItem);
                boxItems.InsertItemsAx(insertIdx, selected);
                box.SelectedItemsAdd(selected);

                TargetBoxItemsRefresh(box, boxItemsSource);
                box.ScrollIntoViewIndex(insertItem == null ? int.MaxValue : box.SelectedIndex);
            }
            catch (Exception ex) { MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace); }
            return true;
        }
Example #3
0
 /// <summary>選択アイテム追加・挿入</summary>
 public bool bxAddItems(ListBox src, ListBox target, bool IsInsert = false, IList trgItemsSource = null)
 {
     if (src == null) return false;
     //
     return bxAddItems(src.SelectedItemsList(true), target, IsInsert, trgItemsSource);
 }