如何对列表框进行排序 [英] how to sort a listbox

查看:107
本文介绍了如何对列表框进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何对列表框进行排序?

这是我在msdn中找到的内容,但似乎我必须在?.Net
上添加引用

How can I sort the listbox?

This is what I have found in the msdn, but it seems that I have to add a refrence to ?.Net

listBox2.Items.SortDescriptions.Add(new SortDescription(
                "Content", ListSortDirection.Descending));


请帮助.


Plese help.

推荐答案

IMO这是错误的方法,ListBox应该仅用于显示数据.如果数据应显示为已排序,则应首先对其进行排序,然后将其添加到控件中.

将您的项目添加到集合中(可能是List<T>),然后根据需要进行排序,然后将项目添加到ListBox中.
IMO this is the wrong approach, the ListBox should be used for displaying data only. If the data should be shown sorted it should be sorted first then added to the control.

Add your items to a collection (List<T> maybe) and sort that however you wish then add the items to the ListBox.


ListBox.Sorted [

您可以设置将listbox属性设置为true,或者可以将列表框的所有项目都保存到arraylist中.使用foreach循环将它们添加到arraylist.然后对数组进行排序.并将它们再次添加到列表框中.
You can either set the listbox property to true or you can get all the items of the listbox to an arraylist. With a foreach loop add them to the arraylist. Then sort the array. and add them sorted to the listbox again.


这篇关于如何对列表框进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆