【问题标题】:Listview disable certain check box once checkedListview 一旦选中就禁用某些复选框
【发布时间】:2018-01-16 04:31:34
【问题描述】:

请帮帮我!
我有一个启用了复选框的ListView。我需要禁用所有选中的项目复选框,用户不应尝试再次单击它。 这是我的代码,我遇到了错误。

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

【问题讨论】:

    标签: vb.net listview checkbox


    【解决方案1】:

    如果我正确理解了您的隐含逻辑,则一旦检查了 ListviewItem 并包含 Text 属性等于“已完成”的 ListViewSubItem,您不希望用户能够取消选中该项目。 “已完成”子项的添加是在按钮单击事件处理程序中执行的,如下所示:

    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
    

    据我所知,没有办法直接禁用ListViewItem 以防止它被取消选中。但是,ListView 确实具有ItemCheck 事件,可用于防止更改“已检查”状态。如果“UnChecked”项的 SubItem 的文本为“Completed”,则以下代码会阻止 Checked 状态更改。

    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
    

    【讨论】:

    • @tharun 如果是这种情况,请考虑支持答案。您已成为会员 3 年,但从未对 Q 或 A 投票。支持您认为有帮助或信息丰富的帖子有助于其他人找到好帖子。如果您无法发布答案,这是一种帮助他人的方式。简短的tour 解释了更多。
    • 当然是 Plutonix :)
    猜你喜欢
    • 2013-07-24
    • 1970-01-01
    • 2013-04-19
    • 1970-01-01
    • 2020-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-19
    相关资源
    最近更新 更多