Problem
How can I add both Text and Value to the items of my ComboBox in a C# WinApp? I ran a search and most of the answers mentioned “binding to a source”, but I don’t have a binding source in my software… How do I go about doing something like this:
combo1.Item[1] = "DisplayText";
combo1.Item[1].Value = "useful Value"
Asked by Bohn
Solution #1
To return the text you desire, you must construct your own class type and override the ToString() method. Here’s an example of a simple class you can use:
public class ComboboxItem
{
public string Text { get; set; }
public object Value { get; set; }
public override string ToString()
{
return Text;
}
}
A basic example of its application is as follows:
private void Test()
{
ComboboxItem item = new ComboboxItem();
item.Text = "Item text1";
item.Value = 12;
comboBox1.Items.Add(item);
comboBox1.SelectedIndex = 0;
MessageBox.Show((comboBox1.SelectedItem as ComboboxItem).Value.ToString());
}
Answered by Adam Markowitz
Solution #2
// Bind combobox to dictionary
Dictionary<string, string>test = new Dictionary<string, string>();
test.Add("1", "dfdfdf");
test.Add("2", "dfdfdf");
test.Add("3", "dfdfdf");
comboBox1.DataSource = new BindingSource(test, null);
comboBox1.DisplayMember = "Value";
comboBox1.ValueMember = "Key";
// Get combobox selection (in handler)
string value = ((KeyValuePair<string, string>)comboBox1.SelectedItem).Value;
Answered by fab
Solution #3
You can use anonymous classes in the following way:
comboBox.DisplayMember = "Text";
comboBox.ValueMember = "Value";
comboBox.Items.Add(new { Text = "report A", Value = "reportA" });
comboBox.Items.Add(new { Text = "report B", Value = "reportB" });
comboBox.Items.Add(new { Text = "report C", Value = "reportC" });
comboBox.Items.Add(new { Text = "report D", Value = "reportD" });
comboBox.Items.Add(new { Text = "report E", Value = "reportE" });
UPDATE: Although the preceding code will appear correctly in a combo box, you will not be able to use the ComboBox’s SelectedValue or SelectedText properties. To use them, bind the combo box as follows:
comboBox.DisplayMember = "Text";
comboBox.ValueMember = "Value";
var items = new[] {
new { Text = "report A", Value = "reportA" },
new { Text = "report B", Value = "reportB" },
new { Text = "report C", Value = "reportC" },
new { Text = "report D", Value = "reportD" },
new { Text = "report E", Value = "reportE" }
};
comboBox.DataSource = items;
Answered by buhtla
Solution #4
To resolve combobox items in real time, you should utilize a dynamic object.
comboBox.DisplayMember = "Text";
comboBox.ValueMember = "Value";
comboBox.Items.Add(new { Text = "Text", Value = "Value" });
(comboBox.SelectedItem as dynamic).Value
Answered by Mert Cingoz
Solution #5
Instead of building a custom class, you may use Dictionary Object to add text and value to a Combobox.
In a Dictionary Object, add keys and values:
Dictionary<string, string> comboSource = new Dictionary<string, string>();
comboSource.Add("1", "Sunday");
comboSource.Add("2", "Monday");
Combobox should be bound to the source Dictionary object:
comboBox1.DataSource = new BindingSource(comboSource, null);
comboBox1.DisplayMember = "Value";
comboBox1.ValueMember = "Key";
Key and value to be retrieved:
string key = ((KeyValuePair<string,string>)comboBox1.SelectedItem).Key;
string value = ((KeyValuePair<string,string>)comboBox1.SelectedItem).Value;
Combobox Text and Value (Full Source)
Answered by cronynaval
Post is based on https://stackoverflow.com/questions/3063320/combobox-adding-text-and-value-to-an-item-no-binding-source