简体   繁体   中英

Append a string to each item of a comboBox

I basically populate a comboBox with a range of numbers using the following code:

comboBox1.Items.AddRange(Enumerable.Range(0, 50).Cast<object>().ToArray())

The thing is that I'd like to have the unit of measurement comparing soon after the numbers. Thus my question, how can I add a string to each item of my comboBox?

您可以使用Linq表达式来做到这一点:

comboBox1.Items.AddRange(Enumerable.Range(0, 50).Select(x => x.ToString() + " sufix").Cast<object>().ToArray())

Additionally to the solution provided by Diego Rafauel Souza you could also append the text to each item of the combobox:

for (var index = 0; index < comboBox1.Items.Count; index++)
{
    var item = comboBox1.Items [index];
    comboBox1.Items[index] = $"{item} {suffix}";
}

This simply adds the suffix to every item of the comboBox.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM