I have a list box, with an item "Math". When I click on the Math item, I want a new collection of items to be added to the listbox after removing the current Math item, and make a back button to appear to return Math item back in case I need to go back. I was able to do that, but after I click on the back button and click on the Math item again, the previous collection of items is added to the previously added collection, so I get repeats. I can't figure out how to clear the "MathDep" - previous collection of items before importing the collection of items again. Here is the code I have:

Private Sub ListBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles ListBox1.MouseClick

        If Me.ListBox1.SelectedItem = "Math" Then

            BackButton.Visible = True
            'Clears the current item - "Math"
            ListBox1.Items.Clear()
            'Which Items to add
            MathDep.AddRange(New String() {"Boo, Ashka", "Reffo, James", "Eldin, Carl", "Utkin, Boris"})
                'Adds the new collection of items
                Dim str As String
                For Each str In MathDep
                    ListBox1.Items.Add(str)
                Next
        End If
    End Sub

    Private Sub BackButton_Click(sender As Object, e As EventArgs) Handles BackButton.Click
        If RB_Department_4.Checked = True Then
            ListBox1.Items.Clear()
            'The listbox has the Math item back
            For Each item As String In My.Settings.DepartmentCollection
                ListBox1.Items.Add(item)
            Next
            BackButton.Visible = False
        End If
    End Sub

I can't figure out how to clear the "MathDep" - previous collection of items before importing the collection of items again.

MathDep.Clear()

Since you're using a hardcoded collection, you can just clear MathDep when you clear ListBox1.Items.

Thank you for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.