Vb.net – Disabling checkbox selections in VB .NET 2008 Winform Listview

.net-3.5listviewvb.netvisual-studio-2008winforms

How do you disable additional checkbox selections/deselections without sacrificing the functionality of the ListView? I know you can call: ListView.Enabled = False, but that also disables any scrolling within it.

For example: I have a timer that starts a backup based on the Listview items that are checked. After a certain time, I don't want the end-user to be able to click on any of the checkboxes within the listview (so I have a set number of items to backup), but I do want them to be able to scroll the list while the backup is being performed. I tried this:

Private Sub clboxOptions_ItemChecked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles clboxOptions.ItemChecked

If backupStarted = True Then
   If e.Item.Checked = True Then
      e.Item.Checked = False
   Else
      e.Item.Checked = True
End If

But this doesn't seem to work for me.
Thanks!
JFV

Best Answer

Here is an other method to disable the users click on listviewitem checkbox.

 Public Sub ChangeItemCheckState(ByVal val As Boolean, ByVal index As Integer)
    If Monitor.TryEnter(Me.Items(index), 10) Then
        Try
            Me.Items(index).Checked = val

        Finally
            Monitor.Exit(Me.Items(index))
        End Try
    End If
End Sub

Public Sub ChangeItemCheckState(ByVal val As Boolean, ByVal item As ListViewItem)
    If Monitor.TryEnter(item, 10) Then
        Try
            item.Checked = val
        Finally
            Monitor.Exit(item)
        End Try
    End If
End Sub

Private Sub ListviewOPC_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles Me.ItemCheck
    If Monitor.IsEntered(Me.Items(e.Index)) Then
        '
    Else
        'prevent click from users
        e.NewValue = e.CurrentValue
    End If
End Sub

this method is thread safe. To change the checkedstate of an item you have to call the ChangeItemCheckState methods. If you want to enable/disable the itemcheck by click, you have to add another property.

Private disableUserCheckItem As Boolean
Public Property PreventUserCheckItem() As Boolean
    Get
        Return disableUserCheckItem
    End Get
    Set(ByVal value As Boolean)
        disableUserCheckItem = value
    End Set
End Property

 Public Sub ChangeItemCheckState(ByVal val As Boolean, ByVal index As Integer)
    If Monitor.TryEnter(Me.Items(index), 10) Then
        Try
            Me.Items(index).Checked = val
        Finally
            Monitor.Exit(Me.Items(index))
        End Try
    End If
End Sub

Public Sub ChangeItemCheckState(ByVal val As Boolean, ByVal item As ListViewItem)
    If Monitor.TryEnter(item, 10) Then
        Try
            item.Checked = val
        Finally
            Monitor.Exit(item)
        End Try
    End If
End Sub

Private Sub ListviewOPC_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles Me.ItemCheck
    If Monitor.IsEntered(Me.Items(e.Index)) Then
        'do nothing or other nessesary things.
    Else
        'prevent click from users
        If PreventUserCheckItem Then
            e.NewValue = e.CurrentValue
        End If
    End If
End Sub
Related Topic