计数检查CheckedListBox项目

时间:2010-04-12 11:59:30

标签: .net vb.net

我有CheckedListBox和四个项目! 现在我要计算已检查项目的数量。 为此,我使用:

 countnumber=CheckedListBox1.CheckedItems.Count

但即使我检查了CheckedListBox项目,countnumber仍为0!

我做错了什么?

3 个答案:

答案 0 :(得分:0)

告诉我们你的代码。 CheckedListBox1.CheckedItems.Count应该有效,就像CheckedListBox1.CheckedIndices.Count一样,所以如果您希望我们查明问题,您应该向我们展示一些代码。

答案 1 :(得分:0)

我遇到了类似的问题:如果未进行任何检查并且我在ItemChecked事件中有cbl.CheckedItems.Count,我想禁用按钮。

    Private Sub cbl_Check(sender As Object, e As ItemCheckEventArgs) Handles cbl_overrides.ItemCheck
        Dim count As Integer = cbl.CheckedItems.Count
        ...
    End Sub

在第一个通话中,计数始终为0,但是您检查了第二个项目,则计数为1。

形成文档文档link

  

备注

     

直到ItemCheck事件发生后,检查状态才会更新。

如果您处于ItemChecked事件中,并且想要准确的计数,则需要

    Private Sub cbl_Check(sender As Object, e As ItemCheckEventArgs) Handles cbl_overrides.ItemCheck
        Dim count As Integer = cbl.CheckedItems.Count
        If e.NewValue = CheckState.Unchecked
            count -= 1
        ElseIf e.NewValue = CheckState.Checked
            count += 1
        End If
        ...
    End Sub

注意:这将对处于不确定状态的项目和已检查项目进行计数,因为CheckedItems返回已确定项目和已检查项目。

答案 2 :(得分:0)

简单的答案是,将以下内容放入“ ItemCheck”事件或“ IndexChanged”事件中。 当您选中/取消选中项目时,它会在您前进时提醒当前计数。

MsgBox(CheckedListBox1.CheckedItems.Count)

Dim count as Integer = 0
count = CheckedListBox1.CheckedItems.Count
MsgBox(count)

我实际上在此页面上偶然发现了一种限制选择数量的方法,这确实需要计算选择的数量(相关的种类),但是我可能略有偏离主题。但是,我自己做了几次尝试,在其他地方看到了多种实现方法的方法,并且惊讶于大多数示例过于复杂。我提出以下内容,作为限制选择数量的最简单方法。我绝不是一个专业的程序员,我是一个自学成才的爱好。 但是也许其他人会发现这很有用,因为限制选择数量是您可能想要实现的常见约束。

' VB.NET Example
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Lets say you have 5 x possible options, but only want to allow 2 x selections maximum
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'    1. On the Form itself, Create "1 x CheckedListBox" with "5 x Collection items"
'       (These will represent the "5 x options" to choose from) 

'    2. Create a Global / public Variable to define the "2 x Selection limit" 
'       (as shown in below example, with the variable "choiceLimit")

'    3. The "count" of items is calcualted in the "IndexChanged" event, 
'       because its gets called before the "ItemCheck" event 

'    4. Finally, the actual logic goes in the "ItemCheck" event, 
'       if the current number of checked items is at the limit, 
'       it overrides the event and auto-UNchecks further selections, which enforces the limit. 
'       if you create a new form, add a checklistbox, then copy the code below it works


' Make these Variables Public / Global so they can be accessed by both events below
Dim countItemsChecked As Integer = 0
Dim choiceLimit As Integer = 2

' IndexChanged Event
Private Sub CheckedListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CheckedListBox1.SelectedIndexChanged
    ' whenever an item is selected / reselected it re-counts the number of checked items. 
    countItemsChecked = CheckedListBox1.CheckedItems.Count
End Sub

' ItemCheck Event
Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
    ' if the current checked items is at the limit, then
    If CheckedListBox1.CheckedItems.Count >= choiceLimit Then
        ' if the current item selected was alreday checked...
        If e.CurrentValue = CheckState.Checked Then
            ' ... just uncheck it
            e.NewValue = CheckState.Unchecked
        Else
            ' ... Otherwise alert "Choice Limit Reached"
            MsgBox("Choice limit reached!")
            ' and override the itemCheck State to force it back to being "UNChecked" 
            e.NewValue = CheckState.Unchecked
        End If
    End If
End Sub

并且没有注释,完整的代码是:

Dim countItemsChecked As Integer = 0
Dim choiceLimit As Integer = 2

' IndexChanged Event
Private Sub CheckedListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CheckedListBox1.SelectedIndexChanged
    countItemsChecked = CheckedListBox1.CheckedItems.Count
End Sub

' ItemCheck Event
Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
    If CheckedListBox1.CheckedItems.Count >= choiceLimit Then
        If e.CurrentValue = CheckState.Checked Then
            e.NewValue = CheckState.Unchecked
        Else
            MsgBox("Choice limit reached!")
            e.NewValue = CheckState.Unchecked
        End If
    End If
End Sub