How to delete items from multiple list box by deleting an item associated with all?

advertisements

I want to remove Server1 from it's listbox, i'd want it to remove all the other items in the other listboxes associated with it as well. ("Server-Domain1" and all the "Server1-Domain1-CSR's"). Is there a way to do this?

To "bind" these list boxes i just used:

domainListBox.Items.Add((serverListBox.SelectedItem) + "-" + (this.domainTextbox.Text));

and

        csrListBox.Items.Add((domainListBox.SelectedItem) + ("-CSR-1"));
        csrListBox.Items.Add((domainListBox.SelectedItem) + ("-CSR-2"));
        csrListBox.Items.Add((domainListBox.SelectedItem) + ("-CSR-3"));


If you choose your server from servers listbox, you can remove associated items like this (lets pretend there is some remove button, you selecting domain from listbox and clicking on remove button):

private void removeBtn_Click(object sender, EventArgs e)
    {
        List<string> items = csrListBox.Items.Cast<string>().ToList();
        foreach (string item in csrListBox.Items)
        {
            Regex regex = new Regex(@"^" + domainListBox.SelectedItem + @"\w*");
            Match match = regex.Match(item);
            if (match.Success)
            {
                items.Remove(item);
            }
        }

        csrListBox.DataSource = items;
    }

Hope it helps.