Click here to Skip to main content
15,882,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I need to display multiple items on one line of text in a combo box.
Such as
C#
"One,a,b
 Two,a,b
 Three,a,b"

While the comboBox.Items.Add() only lets one object, I was hoping the AddRange() control
would it, but it doesn't seem to or is my GoogleFu weak this morning as I have done the following:
C#
cboDUTComPort.Items.AddRange(new[] {nameArray[index],"Narf"});

but this Splits onto a new line each time. giving
C#
nameArray[0]
"Narf"
rather than the hoped for
C#
nameArray[0], "Narf"


I am close but...
Posted

1 solution

Have you tried:
C#
cboDUTComPort.Items.Add(string.Join(",", new [] {nameArray[index], "Narf"}));
 
Share this answer
 
Comments
glennPattonWork3 14-May-14 5:06am    
Cheers for that work in the test prog now to alter the beast!!
OriginalGriff 14-May-14 5:21am    
The other solution is to create a class to hold the multiple values and override ToString in that: the combo box item is then the class instance, which means you don't have to parse or split anything later when you want to use it.

public class MyClass
{
public string Name { get; set; }
public int Baud { get; set; }
public int BitsPerChar { get; set; }
public override string ToString()
{
return string.Format("{0}, {1}, {2}", Name, Baud, BitsPerChar);
}
}
...
myComboBox.Items.Add(new MyClass() { Name = "COM1", Baud = 9600, BitsPerChar = 8 });
...
MyClass mc = (MyClass) myComboBox.Items[0];
Console.WriteLine(mc.Name);
glennPattonWork3 14-May-14 5:23am    
Thanks, that appears to have solved this bit, await further panic!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900