1

I'm looking for some help, I'm new with C# and i'm trying to make a combobox with custom indexes, I was adding the items this way

mycb.Items.Add("My value 1");
mycb.Items.Add("My value 2");

by this way I get these indexes by default 0 and 1 so I try to give the indexes that I want to the CB using Insert

mycb.Items.Insert(5,"My value 1");
mycb.Items.Insert(6,"My value 2");

this way the CB should have the indexes 5 and 6 but this is not working because when I select one of those options this error appeared

invalid argument=value of '5' is not valid for 'index'

It works when I add consecutive indexes starting by 0 but that is not what I want, how can I add the indexes that I want to the CB without having this issue? I hope you can help me, thanks.

T.S.
  • 18,195
  • 11
  • 58
  • 78
Professor Zoom
  • 339
  • 1
  • 4
  • 13
  • The indices of the first 2 items are `0` and `1`. What you have is something else - an item value perhaps? Thinking of them as an index isnt helpful because as you documented, you cant use 5 and/or 6 as an index to retrieve the first or second item. – Ňɏssa Pøngjǣrdenlarp Jan 22 '19 at 22:03
  • @WelcomeOverflow yes I know they are 0 and 1 but I want to give the indexes 5 and 6, how can i do that without that error?, those are two ways I tried to add custom indexes, right now I'm using the second way to achieve it not both, I need to use the indexes I want – Professor Zoom Jan 22 '19 at 22:04
  • Indexes are a "Read Only" property of a collection, so you can't set them to be anything than what they are. The "Index" parameter in the "Insert" function allows you to control where you do the insert within the collection, it's not setting the index. – Frank Ball Jan 22 '19 at 23:55
  • Why the downvote? – Professor Zoom Jan 23 '19 at 04:53

2 Answers2

3

You would make a dictionary (or array) of the items, assign it as the datasource for the combobox and set the "DisplayMember" and "ValueMember" of the combobox, like this:

Dictionary<int, string> myDictionary = new Dictionary<int, string>();

myDictionary.Add(1, "string");
myDictionary.Add(2, "string2");
myDictionary.Add(4, "string4");

mycb.DataSource = myDictionary.ToArray();

mycb.DisplayMember = "Value";
mycb.ValueMember = "Key";
beeker
  • 780
  • 4
  • 17
  • thank you! to make it even better: 1) as explained [here](https://stackoverflow.com/a/26006620/2323103), `mycb.DataSource =` should be the last line. 2) I had to use `mycb.DataSource = new BindingSource(myDictionary, null)` – Couitchy Apr 25 '19 at 13:03
1

Maybe adding object instead of custom index?

Referring to MSDN ComboBox.Items documentation

Although the ComboBox is typically used to display text items, you can add any object to the ComboBox. Typically, the representation of an object in the ComboBox is the string returned by that object's ToString method. If you want to have a member of the object displayed instead, choose the member that will be displayed by setting the DisplayMember property to the name of the appropriate member. You can also choose a member of the object that will represent the value returned by the object by setting the ValueMember property. For more information, see ListControl.

Code from MSDN ListControl.ValueMember documentation example

ArrayList USStates = new ArrayList();
USStates.Add(new USState("Alabama", "AL"));
USStates.Add(new USState("Washington", "WA"));
USStates.Add(new USState("West Virginia", "WV"));
USStates.Add(new USState("Wisconsin", "WI"));
USStates.Add(new USState("Wyoming", "WY"));
ListBox1.DataSource = USStates;
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Djordje Nedovic
  • 559
  • 8
  • 20