When Setting the listbox selectionmode to multiextended I observed three ways to select items:
- pressing mouse key while holding shift key
- pressing mouse key while holding ctrl key
- 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 al开发者_如何学Pythonl 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
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.
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
精彩评论