问题描述
大家好,
我不知道如何在文本中间用一些数字对列表框项目进行排序。
例如,我的列表框有填充列表,其中的项目格式如下:
Hello to all,
I don't have idea how to sort listbox items by some number in the middle of the text.
For example, my listbox has populated list with items in the format like:
ListBox1.Items.Add("Plate: " & oPlateName & "; Angle: - " & strAngle & "; Diameter: " & strDiameter)
并且列表必须按以下值排序:
And list has to be sorted by the value:
strAngle
,它位于列表框项目的中间位置。
非常感谢任何帮助。
TK
我的尝试:
我试过的是:
which is somewhere in the middle of the listbox item.
Any help is very appreciated.
TK
What I have tried:
What I have tried is:
Dim orderedList As List(Of String) = (From item In myList Order By Integer.Parse(item.Substring(item.IndexOf("; Angle: - ") + 1)) Descending Select item).ToList
推荐答案
Public Class PlateItem
Public Plate As String
Public Angle As Decimal
Public Diameter As Decimal
Public Overrides Function ToString() As String
Return "Plate: " & Plate & "; Angle: - " & Angle & "; Diameter: " & Diameter
End Function
End Class
'In your function
Dim plateItems As New List(Of PlateItem)
'Loop through items to add
plateItems.Add(New PlateItem with {.Plate = oPlateName, .Angle = strAngle, .Diameter = strDiameter})
'Finish Adding
lstPlateList.Items.AddRange(plateItems.OrderBy(Function(p) p.Angle).ToArray) 'This line had an error. Fixed.
这也可以让你从lstPlate.SelectedItem中提取一个PlateItem对象,你可以拉出角度和diamter from,不需要字符串操作
That will also let you pull out a PlateItem object from lstPlate.SelectedItem which you can pull the angle and diamter from, without needing the string manipulation
'Sorting by Angle
Dim items = (From item In lstPlateList.Items
Let parts = item.ToString.Split(New String() {"; Angle: - ", "; Diameter: "}, StringSplitOptions.None)
Order By CInt(parts(1))
Select item).ToArray
lstPlateList.Items.Clear()
lstPlateList.Items.AddRange(items)
这篇关于如何按列表框项中间的值对列表框项进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!