slayernoah
slayernoah

Reputation: 4492

Stop items in a list view from being selected on right click

I have a ListView on a Windows Form in VB 2010.

I have set the MultiSelect property of the ListView to False so that only one item can be selected at any time.

I have configured a context menu for the ListView and it shows up correctly when the ListView is right clicked.

[Added a ContextMenuStrip control in the Designer and set the ContextMenuStrip property of the ListView to this.]

Consider these 2 scenarios:

  1. A user right clicks on an item that is already selected in the ListView. Then the context menu is displayed and there are no issues.

  2. A user right clicks on an item other than the item that is already selected in the ListView, Then before the context menu is displayed, the item that the user right clicked is selected.

In scenario 2, I need to stop the item that the user right clicks on from being selected automatically. Need to context menu to be displayed but the item that was previously selected should remain selected.

How can I achieve this?

I noticed that on the ListView's MouseDown event, the SelectedItems.Item(0).Index property is still at the old index. However, on the MouseUp event, this property changes to the new index.

In the MouseDown event handler, or anywhere else, how can I stop the SelectedItems from changing? Or how can I change it back to the previous selected item (without the user noticing it is being changed and then changed back)?

I can catch a right click on the MouseDown or MouseUp using the code below. However, I am not sure what I need to put inside this condition to stop the SelectedItems from changing.

    If e.Button = Windows.Forms.MouseButtons.Right Then
        ...
    End If

Note: I am able to use the following code for this. However, when I use this with scenario (2), it selects the item that the user right clicked on and then changes it back to the previous item and this change back is seen by the user. Therefore this solution cannot be used.

Dim intPrevSelectedIndex As Integer = -1
Dim boolCancel As Boolean = False
Private Sub ListView1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
    If ListView1.SelectedItems.Count > 0 AndAlso e.Button = Windows.Forms.MouseButtons.Right Then
            boolCancel = True
            intPrevSelectedIndex = ListView1.SelectedItems(0).Index
        End If
End Sub

Private Sub ListView1_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseUp
    If boolCancel Then
        lstWalkResults.Items(intPrevSelectedIndex).Selected = True
        boolCancel = False
    End If
End Sub

Please let me know any solutions you might have. Thanks for your time!

Upvotes: 2

Views: 1970

Answers (1)

alan
alan

Reputation: 6953

In the code behind you should be able to handle the right click event. In that method you would display the context menu manually and then ignore the click event preventing the item from being selected.

If e.Button = Windows.Forms.MouseButtons.Right Then
    //display context menu because you're handling the click event manually.
    ...context menu code...
    Dim ee As New System.Windows.Forms.MouseEventArgs(Forms.MouseButtons.None, e.Clicks, e.X, e.Y, e.Delta)
    e = ee
End If

Upvotes: 1

Related Questions