在VB中以数字方式对列表框项目进行排序 [英] Sorting Listbox Items numerically in VB

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

问题描述

我需要对视觉基本列表框中的项目进行数字排序,也就是说,我有一个数字集合,我希望对其进行越来越多的排序.

I need to sort the items in a visual basic listbox numerically, that is, I have a collection of numbers I would like to be sorted increasingly.

我试图简单地使用列表框的Sorted属性,但是发现它将列表中的数字视为字符串,也就是说,它将查看第一个数字,然后查看第二个数字,等等,以确定顺序.例如,这意味着13将显示在5之前.

I tried to simply use the listbox's Sorted property, but found that it treated the numbers as if they were strings, that is, it would look at the first digit, then the second, etc. to determine the order. That meant that 13 would show before 5, for example.

我想到了将所有数字转储到数组中,对数组赋值,然后将它们推回列表框,但是,老实说,我不知道如何进行排序.我认为该数组将无用,因为列表框已充当伪数组.

I thought of dumping all the numbers into an array, soring the array, and then pushing them back to the listbox, but, honestly, I don't know how to go about the sorting. I figured the array would be useless, since the listbox already acts as a pseudo array.

有什么想法吗?

推荐答案

您可以使用以下内容:

Private Shared Sub SortIntegerListBox(ByVal listBox As ListBox)
    Dim TempList As New List(Of Integer)
    For Each LI In listBox.Items
        TempList.Add(Integer.Parse(LI.ToString()))
    Next
    TempList.Sort()
    listBox.DataSource = TempList
End Sub

并在绑定后调用它:

    Dim Items As New List(Of Integer)
    Items.Add(1)
    Items.Add(13)
    Items.Add(2)

    Me.ListBox1.DataSource = Items
    SortIntegerListBox(Me.ListBox1)

这篇关于在VB中以数字方式对列表框项目进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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