在綁定到字典的組合框中設置所選項目 (Setting selected item in combobox bound to dictionary)


問題描述

在綁定到字典的組合框中設置所選項目 (Setting selected item in combobox bound to dictionary)

I have a combobox that is bound to a dictionary like this:

Dictionary<int, string> comboboxValues = new Dictionary<int, string>();
comboboxValues.Add(30000, "30 seconds");
comboboxValues.Add(45000, "45 seconds");
comboboxValues.Add(60000, "1 minute");
comboBox1.DataSource = new BindingSource(comboboxValues , null);
comboBox1.DisplayMember = "Value";
comboBox1.ValueMember = "Key";

I'm getting the key from the SelectedItem like this:

int selection = ((KeyValuePair<int, string>)comboBox1.SelectedItem).Key;

So if my user chooses the "45 seconds" option, I get back 45000 and save that value to an XML file.  When my application is loaded, I need to read that value and then automatically set the combobox to match.  Is it possible to do this when I only the key of 45000?  Or do I need to save the value ("45 seconds") to the file instead of the key?


參考解法

方法 1:

Yes you could use just the 45000 

comboBox1.SelectedItem = comboboxValues[45000];

If you know the index then you can use 

comboBox1.SelectedIndex = i;

i is zero based and -1 means no selection.

Or set the SelectedItem

comboBox1.SelectedItem = new KeyValuePair<int, string>(45000, "45 seconds");

private void Form1_Load(object sender, EventArgs e)
{
    Dictionary<int, string> comboboxValues = new Dictionary<int, string>();
    comboboxValues.Add(30000, "30 seconds");
    comboboxValues.Add(45000, "45 seconds");
    comboboxValues.Add(60000, "1 minute");
    comboBox1.DataSource = new BindingSource(comboboxValues, null);
    comboBox1.DisplayMember = "Value";
    comboBox1.ValueMember = "Key";
    comboBox1.SelectedItem = comboboxValues[45000];
}

方法 2:

Simply use 

comboBox1.SelectedValue=45000

and your combo box will be pre selected by using Key

(by bmt22033paparazzoNaveed Yousaf)

參考文件

  1. Setting selected item in combobox bound to dictionary (CC BY-SA 3.0/4.0)

#Combobox #winforms #selecteditem #Dictionary #.net






相關問題

tcl/tk 小部件組合框失去焦點 (tcl/tk widget combobox loses focus)

Flex Combobox 奇怪的問題 (Flex Combobox strange problem)

在組合框中對齊文本 (Align Text in Combobox)

如何同時綁定到 ComboBox 中的兩個不同的依賴屬性? (How do I bind to two different dependency properties in a ComboBox at the same time?)

在綁定到字典的組合框中設置所選項目 (Setting selected item in combobox bound to dictionary)

easyUI datagrid內部編輯組合框無法選擇默認值 (easyUI datagrid inner edit combobox cannot selected default value)

VBA:數據輸入組合框有效,但命令按鈕給出“無效的屬性值”錯誤 (VBA: Data entry into combobox works, but command button gives "Invalid property value" error)

vb.net - 組合框總是選擇第一個值 (vb.net - combo box always select the first value)

打開 ComboBox DropDown 時不要選擇文本輸入 (Do not select text input when ComboBox DropDown is opened)

如何使用相同的數據源設置多個組合框? (How can I set up multiple combo-boxes with the same data source?)

如何使用 QtreeView 在 QComboBox 中設置所選項目 (How to set selected item in QComboBox with QtreeView)

Tkinter 組合框 Python (Tkinter Combobox Python)







留言討論