Disabling checkbox selections in VB .NET 2008 Winform Listview

一笑奈何 提交于 2019-12-25 04:33:53

问题


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


回答1:


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



回答2:


Instead using the built-in CheckBoxes property, you could draw the check boxes yourself.

Google around and find an example of an OwnerDraw ListView. Draw the checkboxes yourself. Add a new property to your ListView (something like ReadOnly). When ReadOnly is true, draw the checkboxes as disabled and ignore the click messages.




回答3:


You could use ObjectListView (which is a wrapper around a normal .NET ListView). It provides a callback, CheckStatePutter, which is called when the user clicks on a checkbox. In that callback, you can decide whether or not to accept the new checkbox value.

This is a recipe describing this process: How do I use checkboxes in my ObjectListView?




回答4:


I found out what my issue was. I was using the 'ItemChecked' instead of the 'ItemCheck' Method. The below code works for me:

Private Sub clboxOptions_ItemCheck(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles clboxOptions.ItemCheck
    Try
        If backupStarted = True Then
            If e.CurrentValue <> e.NewValue Then
                e.NewValue = e.CurrentValue
            Else
                e.NewValue = e.NewValue
            End If
        End If
End Sub



回答5:


I want disable CheckBox in Listview. When I click Button Go. I was using the 'ItemChecked' Method. I use code this:

Public Sub CheckBoxChecked(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs)
    Try
        If bCheckFromEvent Then
            bCheckFromEvent = False
            Return
        End If
        If BrunService Then
            bCheckFromEvent = True
            ListView.Items(e.Item.Index).Checked = Not ListView.Items(e.Item.Index).Checked
        End If

    Catch ex As Exception
            MsgBox("CheckBoxChecked: " & ex.Message, MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly, "ERROR")
    End Try

End Sub


来源:https://stackoverflow.com/questions/827982/disabling-checkbox-selections-in-vb-net-2008-winform-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!