Search code examples
vb.netcheckedlistbox

How To Clear CheckedListBox From Previous Selection - VB.NET


Okay so I have a checkedlistbox that when a user selects a item it will print out that item into word. That works perfect. However, I want to give the user the option to not select anything at all, but when the user does not select an item in the checkboxlist, it still prints out the previous selected item into MS word.

Below is my code:

 Private Sub ExportContactOkButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExportContactOkButton.Click

    Dim i As Integer
    Dim array_Counter_Contacts As Integer
    Dim array_size_Contacts As Integer

    array_Counter_Contacts = 0

    For i = 0 To ExportContactCheckedListBox.Items.Count() - 1
        If ExportContactCheckedListBox.GetItemCheckState(i) = CheckState.Checked Then
            array_size_Contacts = UBound(SelectedContacts)
            ReDim Preserve SelectedContacts(array_size_Contacts + 1)
            SelectedContacts(array_Counter_Contacts) = ExportContactCheckedListBox.Items(i)

            If Search.debugging = True Then
                MsgBox(ExportContactCheckedListBox.Items(i))
            End If

            array_Counter_Contacts += 1
            ExportContactCheckedListBox.Items(i) = ExportContactCheckedListBox.Items(i).ToString.Replace("'", "''")
            If array_Counter_Contacts = 1 Then
                ContactNames = "" & ExportContactCheckedListBox.Items(i) & ""
            Else
                ContactNames = String.Concat(ContactNames & "', '" & ExportContactCheckedListBox.Items(i) & "")
            End If
        End If
    Next

    If Search.debugging = True Then
        MsgBox(ContactNames)
    End If

sConnection.Close()
Dispose()
Me.Close()
End Sub

I have tried remove, clear, and I even tried this

Dim i As Integer
    For i = 0 To ExportContactCheckedListBox.CheckedIndices.Count - 1
    ExportContactCheckedListBox.SetItemChecked(ExportContactCheckedListBox.CheckedIndices(0), False)
    Next i

But nothing is working. Can anyone help me? All I want is to be able to have the checkedlistbox forget or clear the checked item after the "OK" button is pressed and the text has already been printed into word.


Solution

  • Use a List(Of String) to store the selection and, of course remember, to reinitialize the list when you hit the ExportContactOkButton

    ' Declared at the form level....
    Dim SelectedContacts as List(Of String)
    
    .....
    Private Sub ExportContactOkButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExportContactOkButton.Click
    
        Dim i As Integer
    
        SelectedContacts = new List(Of String)()
    
        For i = 0 To ExportContactCheckedListBox.Items.Count() - 1
            If ExportContactCheckedListBox.GetItemCheckState(i) = CheckState.Checked Then
                SelectedContacts.Add(ExportContactCheckedListBox.Items(i))
                .....
            End If
        Next
    End Sub    
    

    In this way, every time you hit the ExportContactOKButton you reinitialize your list of contacts, loop through the checked items, add the checked one to your list.

    A List(Of String) is better because you don't need to know in advance how many items your user selects in the CheckedListBox and continuosly resize the array. You simply add new items to it. And you could always use it like an array

    Dim Dim array_Counter_Contacts = SelectedContacts.Count
    For i = 0 to array_Counter
       Console.WriteLine(SelectedContacts(i))
    Next