Andy Cheese Andy Cheese - 1 year ago 123 Question

checkedlistbox 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 Source

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
            Process_FromList_URLs_btn.Enabled = False
     End If
 End Sub
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download