【发布时间】:2015-06-06 22:39:23
【问题描述】:
好的,所以我有两个组合框,一个带有修饰符(ctrl、alt、shift、windows 键),另一个带有键(A-Z、F1-F12)。 我想将这些组合框的默认值更改为保存在“Properties.Settings.Default.*”中的值,只是它不起作用。
这是填充组合框的代码:
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;
}
在代码的底部,您可以看到我尝试设置默认值的方式,但所有这些方式都失败了。
设置:
启动窗口:
【问题讨论】:
-
至少对于
Keys的SelectedItem选项看起来工作正常。在您从设置中分配值之前可能发生了一些异常,请检查about Exceptions inLoadevent -
或检查设置值是否与预期相同