Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

listbox

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

like image 410
tomfox66 Avatar asked Oct 18 '25 19:10

tomfox66


2 Answers

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.

like image 51
Lukasz Madon Avatar answered Oct 20 '25 07:10

Lukasz Madon


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
like image 37
Carter Medlin Avatar answered Oct 20 '25 08:10

Carter Medlin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!