Prevent ListView Cancellation

goal

If there are 1 or more items in the list and the user clicks in the ListView space, the selected item should remain selected.

In other words, if an item is selected, it must remain selected if no other item is selected.

Current situation

I have a ListView HideSelection set to false property that will make the selected ListViewItem selected when the control loses focus. However, this does not solve the problem when I press spaces in Listview.

Any suggestions?

+3
source share
1 answer

You can achieve this by subclassing ListView :

 Public Class UIListView Inherits ListView Private Sub WmLButtonDown(ByRef m As Message) Dim pt As Point = New Point(m.LParam.ToInt32()) Dim ht As ListViewHitTestInfo = Me.HitTest(pt) If (ht.Item Is Nothing) Then m.Result = IntPtr.Zero Else MyBase.WndProc(m) End If End Sub Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) Select Case m.Msg Case WM_LBUTTONDOWN Me.WmLButtonDown(m) Exit Select Case Else MyBase.WndProc(m) Exit Select End Select End Sub Private Const WM_LBUTTONDOWN As Integer = &H201 End Class 
+4
source

All Articles