vb.netlistboxitemschecklistbox

How to set items check in checklistbox?


I use SetItemChecked method to automatically check the checklistbox but it does not work. Anyone have a look to my code and please give a solution?

Private Sub Check(value As Double)
    If 0 <= value < 20 Then
        Me.CheckedListBox1.SetItemChecked(0, True)
    ElseIf 20 <= value < 40 Then
        Me.CheckedListBox1.SetItemChecked(1, True)
    ElseIf 40 <= value < 60 Then
        Me.CheckedListBox1.SetItemChecked(2, True)
    ElseIf 60 <= value < 80 Then
        Me.CheckedListBox1.SetItemChecked(3, True)
    Else
        Me.CheckedListBox1.SetItemChecked(4, True)
    End If
End Sub

Then I call check(60) and it returns the 1st item check. It is wrong?


Solution

  • You need to correct your conditions as shown below also Clear

    Private Sub Check(value As Double)
            Me.CheckedListBox1.ClearSelected()
            If 0 <= value And value < 20 Then
                Me.CheckedListBox1.SetItemChecked(0, True)
            ElseIf 20 <= value And value  < 40 Then
                Me.CheckedListBox1.SetItemChecked(1, True)
            ElseIf 40 <= value  And value < 60 Then
                Me.CheckedListBox1.SetItemChecked(2, True)
            ElseIf 60 <= value And value< 80 Then
                Me.CheckedListBox1.SetItemChecked(3, True)
            Else
                Me.CheckedListBox1.SetItemChecked(4, True)
            End If
        End Sub