Set the Text and Value ComboBox Property During Development

I need to add the following items to the combo box.

DisplayText Value

Mpost Added

Call call

RScan Rescan

These elements are quite static and cannot be retrieved from any database ... Therefore, the thought of assigning them at the very time of development.

I cannot use the Items property, because it only requests one value for an item ... Could you help.

Ps: If you offer a BindingSource, could you give me an example. I could not find him.

+3
source share
2 answers

You can use BindingList<KeyValuePair<string, string>>:

var items = new BindingList<KeyValuePair<string, string>>();

items.Add(new KeyValuePair<string, string>("Mpost", "Posted"));
items.Add(new KeyValuePair<string, string>("Call", "Calling"));
items.Add(new KeyValuePair<string, string>("RScan", "Re-Scan"));

myComboBox.DataSource = items;
myComboBox.ValueMember = "Key";
myComboBox.DisplayMember = "Value";

, , . ( InitializeComponent - , , , ComboBox ), .

, : , , , , . ; ComboBox - . , , , . , "/" . - ( ).

+14
private class Data
{
    public string Name { get; set; }
    public string Value { get; set; }
}
public Form1()
{
    InitializeComponent();
    comboBox1.Items.Add(new Data{Name = "Test", Value = "Hello"});
    comboBox1.Items.Add(new Data {Name = "Test2", Value = "World"});
    comboBox1.DisplayMember = "Name";
    comboBox1.ValueMember = "Value";
}

, DisplayMember, , ValueMember.

Daniel

0

Source: https://habr.com/ru/post/1743162/


All Articles