Example #1
0
 public void sourceBoxAllowKeyAction(ListBox box, KeyEventHandler add_handler = null)
 {
     if (box == null) return;
     //
     box.PreviewKeyDown += new KeyEventHandler((sender, e) =>
     {
         if (Keyboard.Modifiers != ModifierKeys.None) return;
         //
         switch (e.Key)
         {
             case Key.Enter:
                 (add_handler ?? button_Add_Click)(sender, e);
                 //一つ下へ移動する。ただし、カーソル位置は正しく動かない。
                 int pos = box.SelectedIndex + 1;
                 box.SelectedIndex = Math.Max(0, Math.Min(pos, box.Items.Count - 1));
                 box.ScrollIntoViewIndex(box.SelectedIndex);
                 e.Handled = true;
                 break;
         }
     });
 }
Example #2
0
        /// <summary>選択アイテム削除</summary>
        public bool bxDeleteItems(ListBox box, IList boxItemsSource = null)
        {
            try
            {
                if (box == null || box.SelectedIndex < 0) return false;

                var boxItems = boxItemsSource ?? box.Items;
                int newSelectedIndex = -1;
                while (box.SelectedIndex >= 0)
                {
                    newSelectedIndex = box.SelectedIndex;
                    boxItems.RemoveAt(newSelectedIndex);
                    TargetBoxItemsRefresh(box, boxItemsSource);
                }

                if (box.Items.Count != 0)
                {
                    box.SelectedIndex = Math.Min(newSelectedIndex, box.Items.Count - 1);
                    box.ScrollIntoViewIndex(box.SelectedIndex);
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace); }
            return true;
        }
Example #3
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 #4
0
        /// <summary>選択アイテム追加・挿入</summary>
        public bool bxAddItems(IEnumerable srcList, ListBox target, bool IsInsert = false, IList trgItemsSource = null)
        {
            try
            {
                if (srcList == null || srcList.Cast<object>().Count() == 0 || target == null) return false;

                var trgItems = trgItemsSource ?? target.Items;
                var addList = srcList.Cast<object>()
                    .Where(item => IsEnableDuplicate(item) == true || bxContains(target.Items, item) == false)
                    .Select(item => IsEnableDuplicate(item) == true ? CloneObj(item) : item).ToList();

                int scrollTo = target.SelectedIndex;
                if (IsInsert == true && target.SelectedIndex >= 0)
                {
                    trgItems.InsertItemsAx(target.SelectedIndex, addList);
                }
                else
                {
                    scrollTo = trgItems.AddItemsAx(addList);
                }

                target.UnselectAll();
                TargetBoxItemsRefresh(target, trgItemsSource);
                target.SelectedItemsAdd(addList);
                if (target.SelectedIndex >= 0) target.ScrollIntoViewIndex(scrollTo);
            }
            catch (Exception ex) { MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace); }
            return true;
        }