Kiran Rai Chamling
Kiran Rai Chamling

Reputation: 498

How to delete same items from multiple list box?

I am facing a problem while deleting the items from multiselect list boxes. I will try my best to explain my front end and the logic. Please let me know the correct way to get the output.

Well I need to add new subjects into a list box. I can select multiple items once added into it. I have 3 list boxes first listbox is for holding the major subjects. The second one is for holding the optional subjects. I have two text boxes that feeds the data into the list box, the items on the text box will be added when the button or enter key is pressed.

The third listbox is disabled. It takes the items from the majorsub and optsubs listbox. This means if I add 10 major subjects and 5 optional subjects then the 3rd list will hold this 15subjects. Now I can easily multi select from one list and delete it. Deleting individually is not a problem.

My concern here is - once I select the items from this two list and click delete button it has to search the same items from the third list and remove it from this list as well.

How can I do that? Please help me in this.

Add items in list 1 Remove items from list 1 and 3

This is for inserting and removing the major subs

Public Sub btnMajSubAdd_Click() Handles btnMajSubAdd.Click
    Try
        'CODE TO ADD MAJOR SUBJECTS IN TO THE LIST AND THE ALL SUBJECTS LIST
        If btnMajSubAdd.Text = "+" Then
            If txtMajSubs.Text <> "" Then
                lbMajorSubs.Items.Add(Trim(txtMajSubs.Text))
                lbAllSubs.Items.Add(Trim(txtMajSubs.Text))
                txtMajSubs.Text = ""
                txtMajSubs.Focus()
            End If
        Else
            For n As Integer = 0 To lbMajorSubs.SelectedItems.Count - 1
                ' REMOVE THE CURRENT SELECTED ITEM FROM ITEMS
                For i As Integer = 0 To lbAllSubs.Items.Count - 1
                    If lbAllSubs.Items(i).ToString = lbOptSubs.SelectedItems(n).ToString Then
                        lbAllSubs.Items.Remove(lbAllSubs.Items(i))
                        lbMajorSubs.Items.Remove(lbMajorSubs.SelectedItems(n))
                        i = i - 1
                        n = n - 1
                    End If
                Next i
            Next n
            btnMajSubAdd.Text = "+"
            txtMajSubs.Focus()
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Upvotes: 1

Views: 106

Answers (1)

user1342582
user1342582

Reputation:

The recommended way to do this is with the Model-View-Controller pattern. Instead of adding an item directly to the list box, add it to a collection, then refresh the list box with items in the collection.

In this particular case, maintain two collections, to which you add major subjects and minor subjects, respectively. On adding a new subject, you add it to the appropriate collection, then immediately refresh the list box.

Upon removing an item from the list box, remove the corresponding item from the collection, and refresh the list box again.

Upvotes: 1

Related Questions