是的,这一定是我曾经遇到的最奇怪的要求。我有三个DateTimePickers,用户可以使用箭头更改日,月和年(每一个)。左/右用于选择日/月/年,向上/向下用于增加或减少。就像默认一样。
现在客户希望这是向上/向下反转,也就是说,如果我有第23天并按下,则应为22.如果月份是3月,我按下,则应该是2月。客户总是对的,是吧?
是否有一些默认设置或我可以为此做些什么,或者我是否必须编写自己的"逻辑"对此?
编辑: 一个子问题;如何查看当前选定的值是日,月还是年?我有这个:
Private Sub InvertedDateTimePicker(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles _
DTP1.KeyDown, DTP2.KeyDown, DTP3.KeyDown
Dim dtp As DateTimePicker = sender
If e.KeyCode = 38 Then 'UP
'...if the current selection is day.
'and actually i have to have -2 since the arrow up gave it +1....
dtp.Value = dtp.Value.AddDays(-1)
'...if the current selection is month.
'and actually i have to have -2 since the arrow up gave it +1....
dtp.Value = dtp.Value.AddMonth(-1)
ElseIf e.KeyCode = 40 Then 'DOWN
End If
End Sub
答案 0 :(得分:0)
没有内置的方法来做这个AFAIK。
但是这个怎么样。它确定哪个值已经改变并反转它:
Private _lastDate As Date
Private _keyDown As Boolean
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
_lastDate = DateTimePicker1.Value
End Sub
Private Sub DateTimePicker1_KeyDown(sender As Object, e As KeyEventArgs) Handles DateTimePicker1.KeyDown
_keyDown = True
End Sub
Private Sub DateTimePicker1_KeyUp(sender As Object, e As KeyEventArgs) Handles DateTimePicker1.KeyUp
_keyDown = False
End Sub
Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
If DateTimePicker1.Value = _lastDate Then Exit Sub
If Not _keyDown Then Exit Sub
If _lastDate.Day <> DateTimePicker1.Value.Day Then
'day has changed
Dim days As Integer = DateTimePicker1.Value.Day - _lastDate.Day
_lastDate = _lastDate.AddDays(-1 * days)
End If
If _lastDate.Month <> DateTimePicker1.Value.Month Then
'month has changed
Dim months As Integer = DateTimePicker1.Value.Month - _lastDate.Month
_lastDate = _lastDate.AddMonths(-1 * months)
End If
If _lastDate.Year <> DateTimePicker1.Value.Year Then
'year has changed
Dim years As Integer = DateTimePicker1.Value.Year - _lastDate.Year
_lastDate = _lastDate.AddYears(-1 * years)
End If
DateTimePicker1.Value = _lastDate
End Sub
如果更新代码中的值,则必须在更新之前设置_lastDate,否则它也会被反转!