tags:

views:

74

answers:

2

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

Value DisplayText

Mpost Posted

Call Calling

RScan Re-Scan

These items are fairly static, and is not retrieved from any database... Hence thought of assigning these in design time itself.

I'm Unable to use Items property as it asks only one value per item... Could you please help.

Ps : In case you are suggesting BindingSource, could you please give me an example. I was not able to find one.

+3  A: 

You could use a 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";

I don't think you're going to find a solution much simpler or more straightforward than this. Just put that in your form's constructor (after InitializeComponent--and by the way, I'd attach any event handlers you intend to add to the ComboBox after this point in the code) and you're good.

As for why there isn't an even easier way: honestly, I'd say this is a typical example of how solutions become more complex the more flexible you try to make them. Don't get me wrong; the ComboBox is quite a nice little control. But the thing is, it's designed to take any legitimate backing source, not just a table, or a list, or a collection of items with exactly two properties. Since it can take any data source, the concept of a strict key/value pairing is somewhat stripped of its special status. So it wouldn't make much sense to provide some special GUI for adding items with exactly two properties (though it might be convenient in this particular situation).

Dan Tao
Thanks for the quick reply... But do I necessary need to use Code... Can't I do it in design mode itself, like adding Items... ?
The King
@The King: The problem with design mode (by which I take it you mean using the Windows Forms designer?) is that it only supplies you with access to a GUI designed specifically to supply string values. What you could do is create a DataSet with a DataTable in the Forms designer and add this table as your DataSource. That might work.
Dan Tao
Okay... I'm just wondering why a simple thing is made so complex... !!@@!!
The King
I don't think that seven lines of code are so complex and some things are just not available in the designer.
Oliver
@The King: I really think the code I posted is the easiest option. As for an explanation for the lack of a less "complex" solution, see my edit.
Dan Tao
Thanks... Your explanation is great...
The King
A: 
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";
}

You define the property to be used for display as DisplayMember and the property to be used for the value as ValueMember.

greetings Daniel

Tigraine
Thanks... But do I necessary need to use Code... Can't I do it in design mode itself, like adding Items... ?
The King