【问题标题】:winforms dynamic combobox default valuewinforms动态组合框默认值
【发布时间】: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;
}

在代码的底部,您可以看到我尝试设置默认值的方式,但所有这些方式都失败了。

设置:

启动窗口:

【问题讨论】:

  • 至少对于KeysSelectedItem 选项看起来工作正常。在您从设置中分配值之前可能发生了一些异常,请检查about Exceptions in Load event
  • 或检查设置值是否与预期相同

标签: c# winforms combobox


【解决方案1】:

我建议在Properties.Settings.Default.IndexModifierProperties.Settings.Default.IndexKey(输入int)中存储相应ComboBox 控件的选定索引,例如:

Properties.Settings.Default.IndexKey=comboboxClickerKey.SelectedIndex;
Properties.Settings.Default.Save();

并分别使用该索引值操作以强制返回ComboBox 控件以显示适当的项目,例如:

comboboxClickerKey.SelectedIndex=Properties.Settings.Default.IndexKey

代码会更简洁,不包括大量的类型转换。此外,这里是描述各种 ComboBox 数据绑定技术的文章的链接(与 ASP.NET 相关的示例也可以很容易地适用于 WinForms 应用程序):http://www.codeproject.com/Tips/214418/Binding-DropDownList-to-various-data-structures

注意:关于您的代码 sn-p,最接近工作解决方案的是以下行:

comboboxClickerKey.Text = Properties.Settings.Default.Key.ToString();

接下来的 3 行看起来不正确,很可能会导致异常。如果您没有明确绑定DataTextFieldDataValueField,它可能会起作用;否则可能会失败。

希望这会有所帮助。

【讨论】:

  • 如果你需要其他代码中的默认键,没有组合框怎么办?
  • 如果是这种情况,那么他可以通过相同的索引从该数组(Keys[] 等)中获取 Key(或任何对象)。问候,
  • 抱歉,comboboxClickerKey.SelectedItem = Properties.Settings.Default.Key; 行正常工作。
  • 在这种情况下它可能会工作,但如果 DataValueField 和 DataTextField 分别绑定会失败。正如我已经提到的,产生相当紧凑的代码的最安全和最通用的方法是存储 ComboBox 选择的索引。最好的问候,
  • @AlexBell 我根据您的回答更改了我的代码并且它有效。现在有一个不同的问题,一旦我关闭表单,即使我没有在代码中的任何地方重置值,值也会重置为默认值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-17
  • 2012-07-25
  • 2012-04-07
  • 2014-02-14
  • 2011-08-23
相关资源
最近更新 更多