private void Listbox_MouseUp(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.Left) { return; } ThisIndexHasBeenChangedProgrammatically = -1; if (Listbox.SelectedIndex == -1) //true when nothing is selected { } else if (!CheckIfThumbMouseButtonsHaveBeenPressed(e)) { if (Listbox.Items.Count > 0) { Listbox.MouseDown -= Listbox_MouseDown; _mouseUpIndex = Listbox.IndexFromPoint(Listbox.PointToClient(Cursor.Position)); HandleSelectionChange(); if (Listbox.SelectedIndex != -1) { OnSelectionReady?.Invoke(this, EventArgs.Empty); } } } Listbox.SelectedIndexChanged += Listbox_SelectedIndexChanged; }
private void Listbox_MouseDown(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.Left) { return; } Listbox.SelectedIndexChanged -= Listbox_SelectedIndexChanged; if (Listbox.Items.Count <= 0) { return; } OnSelectionNotReady?.Invoke(this, EventArgs.Empty); if (CheckIfThumbMouseButtonsHaveBeenPressed(e)) { return; } _mouseDownIndex = Listbox.IndexFromPoint(e.Location); //if selection starts beneath the listbox, we don't want anything selected, //since the selection is "buggy" and doesn't select as one would assume. if (_mouseDownIndex != -1) { return; } if (Listbox.SelectedIndex != -1) { Listbox.SetSelected(Listbox.SelectedIndex, false); } Listbox.Enabled = false; Listbox.Enabled = true; }