Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

arrays - C# - is it possible to arrange ComboBox Items from a to z?

I have a very messy long items full of strings in a combobox, and it would be lovely to just sort it from a to z to make it easier to track. Is it possible?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are two possible ways that I could think of:

A) Use the WinForms Combobox Sorted Property

If you're using WinForms, you can use ComboBox.Sorted = true;

ComboBox.Sorted

B) Manually Sort your List with OrderBy

If the data in your combo box comes from in a form of a list, use OrderBy to the List of data you are going to put in the ComboBox before setting it.

Here's an example:

var myList = new List<string>() {"q","w","e","r","t","y"};
var sorted = a.OrderBy(c => c).ToArray()
comboBox1.Items.AddRange(sorted);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...