/// <summary> /// Handles only single selection with keyboard. /// </summary> public void HandleKeyUp(int selectedIndex) { OnSelectionNotReady?.Invoke(this, EventArgs.Empty); Listbox.SelectedIndexChanged -= Listbox_SelectedIndexChanged; Listbox.SelectedIndex = selectedIndex; if (Listbox.SelectedIndex == -1) { OnSelectionReady?.Invoke(this, EventArgs.Empty); } else { _mouseDownIndex = selectedIndex; _mouseUpIndex = selectedIndex; SelectionOld = GetListboxItemsSelection(Listbox.Items.Count, Listbox.SelectedIndices); SelectedItemsOrdered?.Clear(); SelectedItemsOrdered = new List <string> { Listbox.SelectedItem.ToString() }; SelectedIndexesOrdered?.Clear(); SelectedIndexesOrdered = new List <int> { Listbox.SelectedIndex }; HandleSelectionChange(); OnSelectionReady?.Invoke(this, EventArgs.Empty); } Listbox.SelectedIndexChanged += Listbox_SelectedIndexChanged; }
public void Cancel(bool unsubscribeToEvents = true) { if (unsubscribeToEvents) { UnsubscribeToEvents(); Listbox = null; } SelectedIndexesOrdered?.Clear(); SelectedItemsOrdered?.Clear(); _mouseDownIndex = -1; _posY = -1; _ctrlPressed = false; _shiftPressed = false; _mouseUpIndex = -1; SelectionOld = null; _shiftSelectionStartIndex = -1; }
/// <summary> /// Returns the selection in order they were selected. Handles all combinations of selections with mouse click, ctrl /// click, shift click and /// combinations of them. It also supports selecting with dragging and selecting items. It DOES NOT support dragging + /// ctrl or shift pressed. /// </summary> /// <param name="selectionType"></param> /// <param name="itemsSelection"></param> /// <param name="selectedIndexesOrderedOld"></param> /// <param name="mouseDownIndex"></param> /// <param name="mouseUpIndex"></param> /// <param name="listbox"></param> /// <returns></returns> private List <int> GetSelectedIndexesInOrder(HowSelectionHasChanged selectionType, IReadOnlyCollection <bool> itemsSelection, List <int> selectedIndexesOrderedOld, int mouseDownIndex, int mouseUpIndex, ListBox listbox) { var output = new List <int>(); switch (selectionType) { case HowSelectionHasChanged.Error: break; case HowSelectionHasChanged.NoChange: output = selectedIndexesOrderedOld; break; case HowSelectionHasChanged.WithClick: SelectedIndexesOrdered?.Clear(); output.Add(mouseUpIndex); _shiftSelectionStartIndex = mouseUpIndex; break; case HowSelectionHasChanged.WithClickDrag: mouseUpIndex = SpecialCase_CheckIfDragSelectionEndedOutsideFormAndCorrectMouseUpIndex(mouseUpIndex, itemsSelection, listbox); mouseDownIndex = SpecialCase_CheckIfDragSelectionStartedOutsideFormAndCorrectMouseDownIndex(mouseDownIndex, itemsSelection, listbox); if (mouseDownIndex < mouseUpIndex) { output.AddRange(MakeSelection(mouseDownIndex, mouseUpIndex, true)); } else if (mouseDownIndex > mouseUpIndex) { output.AddRange(MakeSelection(mouseUpIndex, mouseDownIndex, false)); } else { output.Add(mouseDownIndex); } _shiftSelectionStartIndex = mouseDownIndex; break; case HowSelectionHasChanged.IncreaseWithCtrLandClick: case HowSelectionHasChanged.DecreaseWithCtrLandClick: output.AddRange(IndexSelectOrDeselect(selectedIndexesOrderedOld, mouseUpIndex)); _shiftSelectionStartIndex = mouseUpIndex; break; case HowSelectionHasChanged.WithCtrLandClickDrag: case HowSelectionHasChanged.WithShiftAndClickDrag: case HowSelectionHasChanged.WithShiftAndCtrlClickDrag: output.AddRange(selectedIndexesOrderedOld); RevertSelection(ref listbox, selectedIndexesOrderedOld); SelectedIndexesOrdered.Clear(); break; case HowSelectionHasChanged.WithShiftAndClick: if (_shiftSelectionStartIndex < mouseUpIndex) { output.AddRange(MakeSelection(_shiftSelectionStartIndex, mouseUpIndex, true)); } else if (_shiftSelectionStartIndex > mouseUpIndex) { output.AddRange(MakeSelection(mouseUpIndex, _shiftSelectionStartIndex, false)); } listbox.SetSelected(_shiftSelectionStartIndex, true); //this is not set if item earlier deselected with ctrl. break; case HowSelectionHasChanged.WithShiftAndCtrlClick: output.AddRange(selectedIndexesOrderedOld); if (_shiftSelectionStartIndex < mouseUpIndex) { output.AddRange(MakeSelection(_shiftSelectionStartIndex + 1, mouseUpIndex, true)); //+1 is because it is added already } else if (_shiftSelectionStartIndex > mouseUpIndex) { output.AddRange(MakeSelection(mouseUpIndex, _shiftSelectionStartIndex - 1, false)); //-1 is because it is added already } listbox.SetSelected(_shiftSelectionStartIndex, true); //this is not set if item earlier deselected with ctrl. break; default: throw new ArgumentOutOfRangeException(nameof(selectionType), selectionType, null); } return(output); }