简体   繁体   English

winforms动态组合框默认值

[英]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 (AZ, F1-F12). 好的,所以我有两个组合框,一个组合框装有修饰符(Ctrl,Alt,Shift,Windows键),另一个组合有键(AZ,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. 我想将那些组合框的默认值更改为保存在“ Properties.Settings.Default。*”中的那些组合框,只是某种程度上它无法正常工作。

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: 设置: http://puu.sh/if5aJ.png

The window on start up: 启动窗口: http://puu.sh/if5jW.png

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 eg: 我建议将对应的ComboBox控件的选定索引存储在Properties.Settings.Default.IndexModifierProperties.Settings.Default.IndexKey (类型int )中,例如:

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: 并分别使用该索引值进行操作,以迫使ComboBox控件返回以显示适当的项目,例如:

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 . 另外,这里是描述各种ComboBox数据绑定技术的文章的链接(与ASP.NET相关的示例也可以轻松地应用于WinForms应用): http : //www.codeproject.com/Tips/214418/Binding-DropDownList到各种数据结构

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. 接下来的3行看起来不正确,很可能会导致异常。 It may work if you don't explicitly bind DataTextField and DataValueField ; 如果不显式绑定DataTextFieldDataValueField ,则可能会起作用。 otherwise it may fail. 否则可能会失败。

Hope this may help. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM