Sorting 对TimeZoneCollection列表进行排序 问题定义
我使用该类在组合框中显示所有可用时区Sorting 对TimeZoneCollection列表进行排序 问题定义,sorting,combobox,timezone,compact-framework,opennetcf,Sorting,Combobox,Timezone,Compact Framework,Opennetcf,我使用该类在组合框中显示所有可用时区 Dim tzc As New TimeZoneCollection Dim TheIndex As Integer Dim MyTimeZoneInfo As New TimeZoneInformation DateTimeHelper.GetTimeZoneInformation(MyTimeZoneInfo) tzc.Initialize() For Each tzi As TimeZoneInform
Dim tzc As New TimeZoneCollection
Dim TheIndex As Integer
Dim MyTimeZoneInfo As New TimeZoneInformation
DateTimeHelper.GetTimeZoneInformation(MyTimeZoneInfo)
tzc.Initialize()
For Each tzi As TimeZoneInformation In tzc
TheIndex = ComboBox1.Items.Add(tzi)
If tzi.StandardName = MyTimeZoneInfo.StandardName Then
ComboBox1.SelectedIndex = TheIndex
End If
Next
' GMT+4:30 = 4 hours and 30 minutes offset
' GMT-4:30 = - 4 hours and 30 minutes offset
但它们没有分类:
' GMT+4:30 = 4 hours and 30 minutes offset
' GMT-4:30 = - 4 hours and 30 minutes offset
' GMT+4:30 = 4 hours and 30 minutes offset
' GMT-4:30 = - 4 hours and 30 minutes offset
如何对列表排序?
' GMT+4:30 = 4 hours and 30 minutes offset
' GMT-4:30 = - 4 hours and 30 minutes offset
字母顺序可以,时间顺序更好
' GMT+4:30 = 4 hours and 30 minutes offset
' GMT-4:30 = - 4 hours and 30 minutes offset
在您这边复制此问题(需要VS和CE设备)
' GMT+4:30 = 4 hours and 30 minutes offset
' GMT-4:30 = - 4 hours and 30 minutes offset
导入OpenNETCF.WindowsCE
公开课表格1
Private Sub Form1_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated
Dim tzc As New TimeZoneCollection
Dim TheIndex As Integer
Dim MyTimeZoneInfo As New TimeZoneInformation
DateTimeHelper.GetTimeZoneInformation(MyTimeZoneInfo)
tzc.Initialize()
For Each tzi As TimeZoneInformation In tzc
TheIndex = ComboBox1.Items.Add(tzi)
If tzi.StandardName = MyTimeZoneInfo.StandardName Then
ComboBox1.SelectedIndex = TheIndex
End If
Next
End Sub
' GMT+4:30 = 4 hours and 30 minutes offset
' GMT-4:30 = - 4 hours and 30 minutes offset
末级' GMT+4:30 = 4 hours and 30 minutes offset
' GMT-4:30 = - 4 hours and 30 minutes offset
我意识到我的问题有点容易解决。因此,我找到了一种简单的方法,使用ArrayList的内置Sort()方法
' GMT+4:30 = 4 hours and 30 minutes offset
' GMT-4:30 = - 4 hours and 30 minutes offset
我所做的:
' GMT+4:30 = 4 hours and 30 minutes offset
' GMT-4:30 = - 4 hours and 30 minutes offset
Dim tzc As New TimeZoneCollection ' the raw collection'
Dim ar_s As New ArrayList() ' the array used to sort alphabetically the DisplayName'
Dim tzc_s As New TimeZoneCollection ' the sorted collection'
Dim TheIndex As Integer
Dim MyTimeZoneInfo As New TimeZoneInformation
DateTimeHelper.GetTimeZoneInformation(MyTimeZoneInfo)
tzc.Initialize()
' copy the display name in an array to sort them'
For Each tzi As TimeZoneInformation In tzc
ar_s.Add(tzi.DisplayName)
Next
ar_s.Sort()
' populated tzc_s, a sorted collection of TimeZoneInformation'
For i As Integer = 0 To ar_s.Count - 1
For Each tzi As TimeZoneInformation In tzc
If ar_s(i) = tzi.DisplayName Then
tzc_s.Add(tzi)
Continue For
End If
Next
Next
' Bind the sorted ArrayList to the ComboBox'
For Each tzi As TimeZoneInformation In tzc_s
TheIndex = ComboBox1.Items.Add(tzi)
If tzi.StandardName = MyTimeZoneInfo.StandardName Then
ComboBox1.SelectedIndex = TheIndex
End If
Next
' GMT+4:30 = 4 hours and 30 minutes offset
' GMT-4:30 = - 4 hours and 30 minutes offset
我将对TimeZoneCollection进行子类化,并添加一个排序方法,就像您以前发现的那样,但或多或少是手工实现的。我不想验证以下内容,因为我这里没有紧凑的框架:
' GMT+4:30 = 4 hours and 30 minutes offset
' GMT-4:30 = - 4 hours and 30 minutes offset
在子类TimeZoneCollection中添加排序方法和IComparable类。在该类中,您可以定义任何排序顺序(按名称、按GMT偏移量…):
' GMT+4:30 = 4 hours and 30 minutes offset
' GMT-4:30 = - 4 hours and 30 minutes offset
因为它只寻找整小时的补偿。所以我需要开发一个新的“解析器”来获取偏差数据
' GMT+4:30 = 4 hours and 30 minutes offset
' GMT-4:30 = - 4 hours and 30 minutes offset
在使用列表框的代码中:
' GMT+4:30 = 4 hours and 30 minutes offset
' GMT-4:30 = - 4 hours and 30 minutes offset
Public Sub fillList()
ComboBox1.Items.Clear()
Dim tzc As New TimeZoneCollection
Dim TheIndex As Integer
Dim MyTimeZoneInfo As New TimeZoneInformation
DateTimeHelper.GetTimeZoneInformation(MyTimeZoneInfo)
tzc.Initialize()
For Each tzi As TimeZoneInformation In tzc
TheIndex = ComboBox1.Items.Add(tzi)
If tzi.StandardName = MyTimeZoneInfo.StandardName Then
ComboBox1.SelectedIndex = TheIndex
End If
Next
End Sub
以上内容按项目的顺序填充列表
' GMT+4:30 = 4 hours and 30 minutes offset
' GMT-4:30 = - 4 hours and 30 minutes offset
Public Sub fillListGMT()
ComboBox1.Items.Clear()
Dim tzc As New myTimeZoneCollection 'subclassed one
Dim TheIndex As Integer
Dim MyTimeZoneInfo As New TimeZoneInformation
DateTimeHelper.GetTimeZoneInformation(MyTimeZoneInfo)
Dim tzc1 As New TimeZoneCollection
tzc1.Clear()
tzc1 = tzc.Initialize()
For Each tzi As TimeZoneInformation In tzc1
TheIndex = ComboBox1.Items.Add(tzi)
If tzi.StandardName = MyTimeZoneInfo.StandardName Then
ComboBox1.SelectedIndex = TheIndex
End If
Next
End Sub
上面的代码填充了按GMT偏移量排序的列表。此答案基于@Josef的想法(用C#编写),并翻译成VB
' GMT+4:30 = 4 hours and 30 minutes offset
' GMT-4:30 = - 4 hours and 30 minutes offset
这是自定义类:
' GMT+4:30 = 4 hours and 30 minutes offset
' GMT-4:30 = - 4 hours and 30 minutes offset
公共类myTimeZoneCollection
继承时区集合
' GMT+4:30 = 4 hours and 30 minutes offset
' GMT-4:30 = - 4 hours and 30 minutes offset
Public Class myTZIComparer
Implements IComparer
' return -1 for a is before b
' return +1 for a is after b
' return 0 if a is same order as b '
Public Function Compare(ByVal a As Object, ByVal b As Object) As Integer _
Implements IComparer.Compare
Dim c1 As TimeZoneInformation = CType(a, TimeZoneInformation)
Dim c2 As TimeZoneInformation = CType(b, TimeZoneInformation)
If (c1.Bias > c2.Bias) Then
Return -1 ' 1; //this will result in reverse offset order'
End If
If (c1.Bias < c2.Bias) Then
Return 1 ' -1;'
Else ' sort by name '
If (c1.DisplayName < c2.DisplayName) Then
Return -1
End If
If (c1.DisplayName > c2.DisplayName) Then
Return 1
Else
Return 0
End If
End If
End Function
End Class
Public Sub MySort()
' Sorts the values of the ArrayList using the specific sort Comparer (by Bias)'
Dim myComparer As IComparer = New myTZIComparer()
Me.Sort(myComparer)
End Sub
我在一台设备上验证了这段代码,它运行良好。这正是我想首先使用IComparer界面执行的操作,但我失败了。我没有实现子类,它看起来是强制性的。不管怎样,我把它翻译成VB,并修改了一些小东西,它工作了。我能用我的验证代码编辑你的答案吗?然后我会接受它作为答案。我让它工作,但不同:我子类TimeZoneCollection,它有一个错误,顺便说一句,在提取标准偏移量。然后我添加了一个按偏移量和显示名排序的IComparer。由于这里没有上传选项,我没有上传完整的项目。并且,很抱歉,在10天之前无法访问代码。请在13号提醒我。10月@josef,没关系,不要上传完整的项目,我得到了一个基于你想法的VB解决方案。我把它作为一个新的答案贴了出来,效果很好。非常感谢。
' GMT+4:30 = 4 hours and 30 minutes offset
' GMT-4:30 = - 4 hours and 30 minutes offset