0
votes

I have a form the stores selected items from a checkedlistbox into an listbox on another form.

'Form2
 Public Sub New(ByVal Citems As List(Of String), ByVal Citems2 As List(Of String))
    InitializeComponent()
    ListBox1.Items.AddRange(Citems.ToArray)
    ListBox2.Items.AddRange(Citems2.ToArray)


 Private Sub Cancel_Click(sender As Object, e As EventArgs) Handles Button2.Click 
'Cancel the form and returns to Form 1

    ListBox1.Items.Clear()
    ListBox1.Refresh()
    ListBox2.Items.Clear()
    ListBox2.Refresh()
    Me.Close()
End Sub

'Form 1
 Dim Citems As New List(Of String)
Dim Citems2 As New List(Of String)


Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    For Each I As String In CheckedListBox1.CheckedItems
        Citems.Add(I)
    Next

    For Each I As String In CheckedListBox2.CheckedItems
        Citems2.Add(I)
    Next

    Dim SecondForm As New Form2(Citems, Citems2)
    SecondForm.Show()

This is the code that I have that is suppose to clear the listboxes and close the form. Problem is that when it is opened again the previously selected items are still in the listbox. How can I clear out the data without complete closing the application?

1
How are you displaying the Form again? Do you have any code in the Load() event of that Form that might be populating the ListBoxes again?Idle_Mind
@Idle_Mind I have add the rest of the code.Alenhj
Looks like it is working as designed: every time Form2 is instanced, you pass it some stuff for the LBs. Your btn Clear event just clears the Listbox, not the source Lists passed - is that what you want it to do?Ňɏssa Pøngjǣrdenlarp
@Plutonix What Form2 is for in a Verification Form to make sure the User selects the correct items they wanted so that is they did not select the right one they can cancel Form2, return to Form1, Select the correct items, and then Form2 would display the items with out the previous item still there. Ex. First selection was: 1, 2, and 4, by they wanted 3 as well so the new selections would be: 1, 2, 3, and 4 instead of 1, 1, 2, 2, 3, 4, and 4.Alenhj
Sounds like the problem is in the first form. The code to clear the listboxes is not needed. They will get cleared when the form closes (last line). if they cancel and return, it looks like Citems.Add(I) and the other will just add new items to that master list the next time they click submitŇɏssa Pøngjǣrdenlarp

1 Answers

0
votes

This works:

 Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click

 Citems.Clear()
For Each I As String In CheckedListBox1.CheckedItems
        Citems.Add(I)
    Next
Citems2.Clear()
    For Each I As String In CheckedListBox2.CheckedItems
        Citems2.Add(I)

    Next

Clears out the list before adding to it. Thanks for the help.