Andy Cheese Andy Cheese - 2 months ago 10
Vb.net Question

checkedlistbox vb.net event to check if selected

If I have a checkedlistbox with items (apple, carrot, frog) and a button that will only be enabled when at least one item is selected from the checkedlistbox, which event do I use?

where 'ListofURLFromDB_Listbox' is the checkedlistbox and 'Process_FromList_URLs_btn' is the button

I've tried .ItemCheck, .SelectedIndexChanged and .SelectedValueChanged

If (ListofURLFromDB_Listbox.SelectedIndex = -1) Then
Process_FromList_URLs_btn.Enabled = False
ElseIf (ListofURLFromDB_Listbox.SelectedIndex > -1) Then
Process_FromList_URLs_btn.Enabled = True
End If


Many thanks

Answer

You can use the SelectedIndexChanged event and count the number of checked items:

 Private Sub ListofURLFromDB_Listbox_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListofURLFromDB_Listbox.SelectedIndexChanged
     If ListofURLFromDB_Listbox.CheckedItems.Count > 0 Then
            Process_FromList_URLs_btn.Enabled = True ' If checked items count is > 0
     Else
            Process_FromList_URLs_btn.Enabled = False
     End If
 End Sub
Comments