0

Setup:

I have tree comboboxes on a Windows Form and they all have the exact same properties set. They have a separate BindingSource and these sources get their items from a DataContext. They all have the same items, only show it in another language. Here's some sample code for the setup:

var allTitles = dbContext.GetTitles();

bindingSourceNL = allTitles.OrderBy(t => t.TitleNL);
DucthBox.DataSource = bindingSourceNL;
//This goes the same for the FR and DE comboboxes
//They have THE SAME set of data, only a different display property

When selecting an item from any of the comboboxes, the other two must get their corresponding title. Each ComboBox has its SelectedIndexChanged-event and calls the same method in which the corresponding title in the other two are set:

public void SetCbValues(object selectedItem)
{
    if (!suspend)
    {
        suspend = true;

        //In real code there's a null-check
        var selectedTitle = (Title)selectedItem;
        DutchBox.SelectedIndex = DutchBox.FindStringExact(selectedTitle.TitleNL);
        FrenchBox.SelectedIndex = FrenchBox.FindStringExact(selectedTitle.TitleFR);
        GermanBox.SelectedIndex = GermanBox.FindStringExact(selectedTitle.TitleDE);

        suspend = false;
    }
}

Binding event handlers:

Binding of the events is done like this:

DutchBox.SelectedIndexChanged += DutchBox_SelectedIndexChanged;
//Same goes for the other two

Unsubscribing is done like this:

DutchBox.SelectedIndexChanged -= DutchBox_SelectedIndexChanged;
//Same goes for the other two

Sample data:

This is only a simulation of representable data that I use:

var titles = new List<Title>
{
    new Title
    {
        TitleNL = "1 - Medewerker",
        TitleFR = "1 - Employé",
        TitleDE = "1 - Mitarbeiter"
    },
    new Title
    {
        TitleNL = "2 - Arbeider",
        TitleFR = "2 - Travailleur",
        TitleDE = "2 - Arbeiter"
    },
    new Title
    {
        TitleNL = "3 - Bediende",
        TitleFR = "3 - Greffier",
        TitleDE = "3 - Schreiber"
    },
}

This list is bound to each ComboBox and depending on which one, the right language is shown.

Problem:

When I change the title in de first ComboBox (Dutch) and press the button on my form, the old values are taken and submitted. When I change one of the other two comboboxes, this doesn't occur. Also when I change a value of the other two, change the Dutch one and press the button, the last chosen value of the other was submitted.

Simulation:

  • On load, selecteditem is null -> select Dutch first item and submit: null is submitted.
  • On load, selecteditem is null -> select French third item -> select Dutch second item and submit: second item is submitted.
  • On load, selecteditem is null -> select third Dutch item -> select Dutch fifth item and submit: null is submitted

Does anyone have an idea what might cause this and how to solve this?

2
  • please show how you bind these comboboxes to event handler. Also does changing selected items in other boxes trigger event?
    – Nogard
    Nov 28, 2013 at 11:34
  • Fro the binding, check my updated answer (section 'binding event handlers'). Yes, the events are triggered on the other ones as well.
    – Abbas
    Nov 28, 2013 at 12:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.