VB.NET-如何限制listview中的复选框选择?

时间:2015-01-12 23:10:39

标签: vb.net listview checkbox

我想将listview中的复选框选项限制为2 求助。

If lvbooklist.CheckedItems.Count >= 2 Then
     e.NewValue = CheckState.Unchecked
     MsgBox("Only 2 Book can be borrow per day")
Else

End If

1 个答案:

答案 0 :(得分:1)

您的代码应该在ItemCheck事件中,并且您应该只是阻止CheckBox从Unchecked状态进入Checked状态(否则当您取消选中某个项目时它们也会收到一条消息你已经选择了两个):

Private Sub lvbooklist_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles lvbooklist.ItemCheck
    If lvbooklist.CheckedItems.Count = 2 AndAlso e.CurrentValue = CheckState.Unchecked Then
        e.NewValue = CheckState.Unchecked
        MessageBox.Show("Only 2 Books can be borrowed per day.")
    End If
End Sub