请帮帮我!
我有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
答案 0 :(得分:2)
如果我正确理解了您的隐含逻辑,一旦选中了ListviewItem
且包含ListViewSubItem
且Text属性等于“已完成”,您不希望用户能够取消选中该项目。添加“已完成”子项是在Button click事件处理程序中执行的,如下所示:
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,则以下代码可防止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