Sorting Listbox Items numerically in VB

后端 未结 2 1236
醉话见心
醉话见心 2020-12-11 09:35

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.

I tried to simply us

2条回答
  •  温柔的废话
    2020-12-11 09:47

    You can use something like this:

    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
    

    And call it after binding:

        Dim Items As New List(Of Integer)
        Items.Add(1)
        Items.Add(13)
        Items.Add(2)
    
        Me.ListBox1.DataSource = Items
        SortIntegerListBox(Me.ListBox1)
    

提交回复
热议问题