listbox selectionmode = multiextended how to get rid of selecting items with mousekey down

14,354

Solution 1

The ListBox class has two SelectionMode. Multiple or Extended.

In Multiple mode, you can select or deselect any item by clicking it. In Extended mode, you need to hold down the Ctrl key to select additional items or the Shift key to select a range of items.

Just proper property need to be set.

Solution 2

You want the "Extended" mode but do not want mouse drag selections unless a shift or control key is pressed. Rather that trying to back out features, you should add features. Try this.

  • Set "KeyPreview" on your form to "True".
  • Set SelectionMode for your ListBox back to "MultiSimple".

Use this code to add the ability to select items when Control or Shift is pressed.

Public Class Form1
    Private bSelectMode As Boolean = False

    Private Sub Form1_KeyUpOrDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown, Me.KeyUp
        bSelectMode = e.Control OrElse e.Shift
    End Sub

    Private Sub ListBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseMove
        If bSelectMode AndAlso e.Button <> Windows.Forms.MouseButtons.None Then
            Dim selectedindex = ListBox1.IndexFromPoint(e.Location)

            If selectedindex <> -1 Then
                ListBox1.SelectedItems.Add(ListBox1.Items(selectedindex))
            End If
        End If
    End Sub
End Class
Share:
14,354
tomfox66
Author by

tomfox66

Updated on June 04, 2022

Comments

  • tomfox66
    tomfox66 almost 2 years

    When Setting the listbox selectionmode to multiextended I observed three ways to select items:

    1. pressing mouse key while holding shift key
    2. pressing mouse key while holding ctrl key
    3. pressing mouse key while moving mouse over unselected item

    1 and 2. is exactly the behaviour I want but I dont' want 3. because later I want to rearrange items by moving all selected items up and down with the mouse.

    How to get rid of 3. ?

    I need a behaviour just like the playlist in Winamp. Rearrange items by dragging and copy paste items