C# - 是否可以从 a 到 z 排列组合框项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17080830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:31:36  来源:igfitidea点击:

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

c#arraysstringcombobox

提问by Hendra Anggrian

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?

我在组合框中有一个非常凌乱的长项目,里面装满了字符串,将它从 a 到 z 排序以使其更容易跟踪会很可爱。是否可以?

采纳答案by aiapatag

There are two possible ways that I could think of:

我能想到的有两种可能的方法:

A) Use the WinForms Combobox SortedProperty

A) 使用 WinForms 组合框Sorted属性

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

如果您使用的是 WinForms,则可以使用 ComboBox.Sorted = true;

ComboBox.Sorted

组合框排序

B) Manually Sort your List with OrderBy

B)手动排序您的列表 OrderBy

If the data in your combo box comes from in a form of a list, use OrderByto the Listof data you are going to put in the ComboBoxbeforesetting it.

如果您的组合框的数据来自一个列表的形式,使用排序依据List数据,你要放在ComboBox之前,设置它。

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);

回答by Hendra Anggrian

turns out I can answer my own question, in Windows 8 C# app, ComboBox has Sort() properties that would simply arrange every items from a to z. Thanks.

原来我可以回答我自己的问题,在 Windows 8 C# 应用程序中,ComboBox 具有 Sort() 属性,可以简单地排列从 a 到 z 的每个项目。谢谢。

回答by Lukas

first I did not populate my dropdown combobox directly like this

首先我没有像这样直接填充我的下拉组合框

AppCombBox1.Items.Add(comboboxvalue);

but instead i populated a list with same data (done in loop in code, which is searching through ini file for values):

但是我用相同的数据填充了一个列表(在代码中循环完成,它在 ini 文件中搜索值):

List<string> TempList = new List<string>();
TempList.Add(comboboxvalue);

after list is populated, I sort it and after that I fill dropdown combobox with sorted values.

列表填充后,我对它进行排序,然后用排序值填充下拉组合框。

    TempList.Sort();
    foreach (string ListValue in TempList)
    {
        AppCombBox1.Items.Add(ListValue);
    }

回答by Adnan Ali

Just set the Sorted Property to True. It will sort the elements. I am fetching values from database and it works fine.

只需设置Sorted Property to True. 它将对元素进行排序。我正在从数据库中获取值并且它工作正常。

enter image description here

在此处输入图片说明