.NET Compact Framework里的DateTimePicker Control显示錯误的month selection list
(華)
作天发现了微軟.NET CF的DateTimePicker Control里的一个问题,就是如果你把PockpetPC的Regional Settings里的Date Format换去'dd-MMM-yyyy'或者用别的Country Settings;比如说'Ducth (Netherlands)'。。。那你DateTimePicker就会出现以下的错误了!
以下就是在DateTimePicker.vb里用来建立month selection list的代码:
Private Sub InitMonthContextMenu()
' create a menu that contains list of months
m_monthMenu = New ContextMenu
Dim i As Integer
For i = 1 To 12
' create new menu item and hookup the click event
Dim item As New MenuItem
m_monthMenu.MenuItems.Add(item)
AddHandler item.Click, AddressOf OnMonthMenuClick
item.Text = DateTime.Parse(String.Format("{0}/1/2000", i)).ToString("MMMM")
Next i
' hookup popup event so can check the current month
AddHandler m_monthMenu.Popup, AddressOf OnMonthMenuPopup
End Sub
' create a menu that contains list of months
m_monthMenu = New ContextMenu
Dim i As Integer
For i = 1 To 12
' create new menu item and hookup the click event
Dim item As New MenuItem
m_monthMenu.MenuItems.Add(item)
AddHandler item.Click, AddressOf OnMonthMenuClick
item.Text = DateTime.Parse(String.Format("{0}/1/2000", i)).ToString("MMMM")
Next i
' hookup popup event so can check the current month
AddHandler m_monthMenu.Popup, AddressOf OnMonthMenuPopup
End Sub
问题就出现在以下这一行代码,因为那Date Format是M/d/yyyy!每当Regional Settings里的Date Format的第一个单为不是M,那就出现错误了。。。
item.Text = DateTime.Parse(String.Format("{0}/1/2000", i)).ToString("MMMM")
只须把这一行代码修改成以下的就不会再有错误的Month selection list了~
Dim d As New Date(2000, i, 1)
item.Text = DateTime.Parse(d).ToString("MMMM")
d = Nothing
item.Text = DateTime.Parse(d).ToString("MMMM")
d = Nothing
以下就是修改了的Month selection list效果。