tharun
tharun

Reputation: 73

Listview disable certain check box once checked

Please help me with this!
I have a ListView with checkboxes enabled. I need to disable all the checked items checkboxes, where the user should not try to click it again. Here is my code, where I am getting error.

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
    Try
        ' submit
        Dim path As String = "C:\Users\jtb43661\Documents\Visual Studio 2017\Projects\IGI Event Tracker\IGI Event Tracker\bin\Debug\Logs\Event.LOG"
        If Not File.Exists(path) Then
            Using sw As StreamWriter = File.CreateText(path)
            End Using
        End If
        Using sw As StreamWriter = File.AppendText(path)
            For Each item In ListView1.CheckedItems
                sw.WriteLine(item.Text & "->" & " Completed-@---> " & Label2.Text)
                item.SubItems.Add("Completed")
                item.BackColor = Color.GreenYellow
                'If item.subItems.text = "Completed" Then
                'here I need to disable or lock the checked checkboxes 

                'End If
            Next
            sw.Close()
        End Using
        MsgBox("Events Submitted Successfully")
    Catch ex As Exception
        MsgBox(ex.Message.ToString)
    Finally
    End Try
End Sub

Upvotes: 0

Views: 1172

Answers (1)

TnTinMn
TnTinMn

Reputation: 11801

If I understand your implied logic correctly, once a ListviewItem is checked and contains a ListViewSubItem with the Text property equal to "Completed" you do not want the user to be able to uncheck that item. The addition of the "Completed" subitem is performed in a Button click event handler something like this:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    For Each itm As ListViewItem In ListView1.CheckedItems
        ' add "Completed" subitem only if it does not currently exist
        Dim hasCompleted As Boolean = False
        For Each subitem As ListViewItem.ListViewSubItem In itm.SubItems
            hasCompleted = subitem.Text.Equals("Completed")
            If hasCompleted Then Exit For
        Next
        If Not hasCompleted Then itm.SubItems.Add("Completed")
    Next
End Sub

As far as I know, there is no way to directly disable a ListViewItem to prevent it from being unchecked. However, the ListView does have the ItemCheck event that can be used to prevent changing the "Checked" state. The following code prevents the Checked state change if the item being "UnChecked" has a SubItem with the Text "Completed".

Private Sub ListView1_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles ListView1.ItemCheck
    If e.CurrentValue = CheckState.Checked Then
        Dim item As ListViewItem = ListView1.Items(e.Index)
        For Each subitem As ListViewItem.ListViewSubItem In item.SubItems
            If subitem.Text.Equals("Completed") Then
                e.NewValue = e.CurrentValue ' do not allow the change
                Exit For
            End If
        Next
    End If
End Sub

Upvotes: 3

Related Questions