开发者

Updating Multiple ComboBoxes

开发者 https://www.devze.com 2023-04-08 06:01 出处:网络
In my project I have about 100 comboboxes, every combobox holds the same items. I want to \"update\" every combobox like this:

In my project I have about 100 comboboxes, every combobox holds the same items. I want to "update" every combobox like this:

When an item is selected in combobox A, all other comboboxes shouldn't hold this item anymore. Likewise, when the selected item from combobox A changes again, the previous item should appear on the other comboboxes again, etc etc, and I want this to happen for every single 开发者_Go百科combobox.

What's the best way to accomplish this? With as less code and without timers, if possible.


Use a helper class that handles the changing and keeps track of the current selected items. Something like this:

public class ComboboxSwitcher
{
    List<ComboBox> boxlist = new List<ComboBox>();
    Dictionary<ComboBox, object> olditems = new Dictionary<ComboBox, object>();

    public void Add(params ComboBox[] boxes)
    {
        boxlist.AddRange(boxes);
        boxes.ToList().ForEach(box => box.SelectedIndexChanged += handler);
    }

    private void handler(object sender, EventArgs e)
    {
        ComboBox trigger = (ComboBox) sender;
        object item = trigger.SelectedItem;
        object olditem = null;
        if (olditems.ContainsKey(trigger)) olditem = olditems[trigger];

        boxlist.ForEach(box =>
                            {
                                if (box != trigger)
                                {
                                    if (olditem != null) box.Items.Add(olditem);
                                    box.Items.Remove(item);
                                }
                            });

        olditems[trigger] = item;
    }
}

Add all combo boxes via the Add method like this:

List<string> items = new List<string> { "A", "B", "C", "D" };
comboBox1.Items.AddRange(items.ToArray());
comboBox2.Items.AddRange(items.ToArray());
comboBox3.Items.AddRange(items.ToArray());
new ComboboxSwitcher().Add(comboBox1, comboBox2, comboBox3);

The class registers a SelectedIndexChanged handler for all comboboxes to be informed of changes.

In case of a selection change it checks, if there is a previously selected value for this combox (using the internal dictionary structure). It then iterates all comboboxes and changes the items, ie. removes the newly selected one and adds the old one to all boxes except the box that had the change. Finally it updates ints internal dictionary.

You didnt need to keep track of current selections in the other comboboxes as the selection there doesn't change.

And you may build distinctive groups of comboboxes by using multiple instances of this class.


I would try it with an DataBinding on the ComboBox so i don't have to control the ComboBoxs itself.
You could control the ComboBox Data by controlling the source directly.
So only show a non marked DataEntry in the DataSource, or something like that.
Sample for DataBinding:
http://msdn.microsoft.com/en-us/library/x8ybe6s2%28v=vs.80%29.aspx


One way....

I think you need three lists: AvailableItems, SelectedItems and ComboBoxes.

When an item is selected, you take it out of AvailableItems and put it in SelectedItems. Then iterate the ComboBoxes and rebind each one to AvailableItems.

The tricky part is having each ComboBox keep it's selected item. Before rebinding, save it's selected item, do the rebinding, put the selected item back and re-select it.

You may need to do a Suspendlayout() while all this is happening to avoid screen updates.

0

精彩评论

暂无评论...
验证码 换一张
取 消