好吧,所以我有兩個組合框,其中一個填充了修飾符(ctrl,alt,shift,windows鍵),另一個帶有鍵(A-Z,F1-F12)。 我想將這些組合框的默認值更改爲保存在「Properties.Settings.Default。*」中的默認值,只是以某種方式它不起作用。winforms動態組合框的默認值
這裏是填充組合框代碼:
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;
}
在你可以看到我試圖設置默認值的方法的代碼的底部,但都未能如願。
設置:
上的窗口啓動:
Atleast for'Keys'選項'SelectedItem'看起來工作正常。在從設置中分配值之前可能會發生一些異常,請查看[關於'Load'事件中的異常](http://stackoverflow.com/questions/3209706/why-the-form-load-cant-catch-exception) – Fabio
或者檢查設置的值是否與預期的相同 – Fabio