Stack Exchange
winforms dynamic combobox default value
Okay, so I have a two combo boxes, one filled with modifiers (ctrl, alt, shift, windows key) and the other one with keys (A-Z, F1-F12).
I want to change the default value of those combo boxes to the one saved in the "Properties.Settings.Default.*", only somehow it's not working.
Here is the code that fills the combo boxes:
private void Settings_Load(object sender, EventArgs e)
{
    KeyModifiers[] modifierArray = new KeyModifiers[] { KeyModifiers.Alt, KeyModifiers.Control, 
                                                        KeyModifiers.Shift, KeyModifiers.Windows };

    var dataSourceModifiers = new List<KeyModifiers>();

    foreach(KeyModifiers modifier in modifierArray )
    {
        dataSourceModifiers.Add(modifier);
    }

    this.comboboxClickerModifier.DataSource = dataSourceModifiers;

    Keys[] keysArray = new Keys[] { Keys.A, Keys.B, Keys.C, Keys.D, Keys.E, Keys.F, Keys.G, Keys.H, Keys.I, Keys.J, Keys.K,
                                    Keys.L, Keys.M, Keys.N, Keys.O, Keys.P, Keys.Q, Keys.R, Keys.S, Keys.T, Keys.U, Keys.V, 
                                    Keys.W, Keys.X, Keys.Y, Keys.Z, Keys.F1, Keys.F2, Keys.F1, Keys.F2, Keys.F3, Keys.F4, Keys.F5,
                                    Keys.F6, Keys.F7, Keys.F8, Keys.F9, Keys.F10, Keys.F11, Keys.F12};

    var dataSourceKeys = new List<Keys>();

    foreach (Keys key in keysArray)
    {
        dataSourceKeys.Add(key);
    }

    this.comboboxClickerKey.DataSource = dataSourceKeys;

    // Down here are the ways I tried to set the default value
    comboboxClickerKey.Text = Properties.Settings.Default.Key.ToString();
    comboboxClickerKey.SelectedIndex = comboboxClickerKey.Items.IndexOf(Properties.Settings.Default.Key);
    comboboxClickerKey.SelectedItem = Properties.Settings.Default.Key;
    comboboxClickerModifier.SelectedItem = Properties.Settings.Default.Modifier;
}

At the bottom of the code you can see the ways I've tried to set the default value, but all of them failed to do so.
The Settings:

The window on start up:


Atleast for Keys option of SelectedItem looks working fine. May be some exception happened before you assign values from settings, check about Exceptions in Load event

Or check values of settings are they same as expected

I would recommend to store in Properties.Settings.Default.IndexModifier and Properties.Settings.Default.IndexKey (type int) just selected indices of corresponding ComboBox controls, like for e.g.:
Properties.Settings.Default.IndexKey=comboboxClickerKey.SelectedIndex;
Properties.Settings.Default.Save();

and respectively, operate with that index value to force back the ComboBox control to display the appropriate item, like:
comboboxClickerKey.SelectedIndex=Properties.Settings.Default.IndexKey
The code will be much cleaner, excluding that numerous type conversions. Also, here is a link to the article describing various ComboBox data binding technique (examples pertinent to ASP.NET could be easily adapted for WinForms app as well): http://www.codeproject.com/Tips/214418/Binding-DropDownList-to-various-data-structures.
Note: regarding your code snippet, the closest to working solution is the line:
comboboxClickerKey.Text = Properties.Settings.Default.Key.ToString();

Next 3 lines do not look right and most likely will cause the exception. It may work if you don't explicitly bind DataTextField and DataValueField; otherwise it may fail.
Hope this may help.

What if you need default key in the other code, where no combobox?

If this is a case, then he can get the Key (or whatever object) from that array (Keys[], etc) by the same index. Regards,

Sorry, but line comboboxClickerKey.SelectedItem = Properties.Settings.Default.Key; working ok.

It may work in this case but fail if DataValueField and DataTextField are bound separately. The safest and most universal way resulting in rather compact code is to store the ComboBox selected index, as I have mentioned already. Best regards,

@AlexBell I changed my code according to your answer and it works. There is a different problem now, once I close the form, the value resets to the default value even though I don't reset the value anywhere in the code

You have to implement Properties.Settings.Default.Save() somewhere in your code, for example, in Windows.Closing event handler. Please accept the answer, and should you have more questions, please post them separately. Best regards,
