Robert
Robert

Reputation: 1726

Remove listbox entries that other listboxes have selected

I've got a simple form which scans my network and finds computers. I dynamically add a user control for each computer found. Within each user control is a dropdown list that I need to manage. Here's a screenshot of the UI: enter image description here

Each listbox may contain 1 of 2 possible lists. For simplicity sake, lets say the left column listboxes are bound to the laptops.xml datasource, while the right column listboxes are bound to the servers.xml.

Lets say the laptops.xml contains the following entries:

  1. Dell Inspiron
  2. Asus
  3. Mac Air

and the servers.xml contain these entries:

  1. Dell Poweredge
  2. HP Tape Backup
  3. Dell Precision
  4. Linux

So what I need the UI to do is whenever the user selects an item from the list, that item should be removed from the other lists so that it can't be selected twice. Note that it should only modify the other lists that are tied to the same list...in other words, if I select 'Mac Air' from the first dropdown, the program should only modify the other two lists and not any of the listboxes tied to the server.xml.

To determine which listbox is bound to which xml file, I use the .Tag property of the listbox when the user control is dynamically created and added to the form.

I would think I can use an ObservableCollection to do this but not sure how to implement it and get it to do what I need.

Upvotes: 0

Views: 48

Answers (1)

Kai Thoma
Kai Thoma

Reputation: 572

You can hold a list of all selected computers (or maybe 2 lists, one for servers, one for laptops) at forms level. Your panels (which contain the comboboxes) should subscribe to the listchangeevent of it and adapt the items of the combobox whenever it changes. And vice versa you need to subscribe to the SelectedIndexChanged-event of the comboboxes for maintaining that global list.

In your form:

ObservableCollection<string> selectedServers = new ObservableCollection<string>();
public void Load()
{
    List<string> allServers = GetServerNames( "servers.xml" );
    foreach( ComputerPanel pnl in serverPanels )
        pnl.LoadLists( allServers, selectedServers );
}

In your panel:

public void LoadLists( List<string> allServers, ObservableCollection<string> selectedServers )
{
    foreach( string server in allServers )
        combo1.Items.Add( server );
    selectedServers.CollectionChanged += selectedServers_CollectionChanged;
    combo1.SelectedIndexChanged += ( object sender, EventArgs e ) => { selectedServers.Add( (string)combo1.SelectedItem ); };
}

private void selectedServers_CollectionChanged( object sender, NotifyCollectionChangedEventArgs e )
{
    string newlySelectedServer = (string)e.NewItems[0];
    if( e.Action == NotifyCollectionChangedAction.Add 
        && (string)combo1.SelectedItem != newlySelectedServer ) //only if selector was not my own combo
        combo1.Items.Remove( newlySelectedServer );
}

(This code is not foolproof, just to give you an idea)

Upvotes: 1

Related Questions