Dim currVal As DateTime
Dim newVal As DateTime
Dim valCheck As Boolean
Dim currSelected As Selection = Selection.None
Public Enum Selection
None = 0
Year = 1
Month = 2
Day = 3
End Enum
Private Sub CheckDTPSelection(dtp As DateTimePicker)
valCheck = True
currVal = dtp.Value
SendKeys.Send("{UP}")
End Sub
Sub RefreshSelection(dtp As DateTimePicker)
If valCheck Then
newVal = dtp.Value
If currVal.Year <> newVal.Year Then
currSelected = Selection.Year
ElseIf currVal.Month <> newVal.Month Then
currSelected = Selection.Month
ElseIf currVal.Day <> newVal.Day Then
currSelected = Selection.Day
End If
dtp.Value = currVal
valCheck = False
End If
End Sub
Private Sub MyDateTimePicker_DropDown(sender As Object, e As EventArgs) Handles MyDateTimePicker.DropDown
RemoveHandler MyDateTimePicker.MouseUp, AddressOf MyDateTimePicker_MouseUp
End Sub
Private Sub MyDateTimePicker_CloseUp(sender As Object, e As EventArgs) Handles MyDateTimePicker.CloseUp
AddHandler MyDateTimePicker.MouseUp, AddressOf MyDateTimePicker_MouseUp
CheckDTPSelection(MyDateTimePicker)
End Sub
Private Sub MyDateTimePicker_KeyUp(sender As Object, e As KeyEventArgs) Handles MyDateTimePicker.KeyUp
If e.KeyValue = Keys.Left OrElse e.KeyValue = Keys.Right Then
CheckDTPSelection(MyDateTimePicker)
End If
End Sub
Private Sub MyDateTimePicker_MouseUp(sender As Object, e As MouseEventArgs) Handles MyDateTimePicker.MouseUp
CheckDTPSelection(MyDateTimePicker)
End Sub
Private Sub MyDateTimePicker_ValueChanged(sender As Object, e As EventArgs) Handles MyDateTimePicker.ValueChanged
Dim dtp As DateTimePicker = DirectCast(sender, DateTimePicker)
RefreshSelection(dtp)
End Sub
Private Sub Btn_WhatsSelected_Click(sender As Object, e As EventArgs) Handles Btn_WhatsSelected.Click
'Show the current selected value in a MessageBox
MessageBox.Show(currSelected.ToString())
End Sub
Enter
事件中添加CheckDTPSelection
(以捕获第一个焦点_无需鼠标_)。2)打开下拉菜单会触发MouseUp
,发送按钮弹起,并更改_可视_选择的日期/月份;因此我会在DropDown
中删除MouseUp
处理程序,然后在CloseUp
中重新添加它。如果您不介意,让我将这些更改添加到您的答案中。 - 41686d6564 stands w. PalestineEnter
事件会导致之前由 MouseUp
引起的相同问题 并且 它在 DropDown
之前触发 (显然), 所以我暂时不会使用 Enter
。 - 41686d6564 stands w. Palestine如果dtp.Focused Then SendKeys.Send("{UP}")
?此外,我认为只有在DTP获得焦点时才会调用CheckDTPSelection
。除非你是在谈论调用该方法和执行SendKeys.Send()
之间非常短的时间段。 - 41686d6564 stands w. PalestineSendKeys
,就像你一样,但考虑到似乎没有其他选择(除了创建自己的 DTP),我不会太过“憎恨”它 :D - 41686d6564 stands w. PalestinePublic MyEventCounter As Integer = 0
Private Sub DTPAcquDt_DropDown(sender As Object, e As EventArgs) Handles DTPAcquDt.DropDown
RemoveHandler DTPAcquDt.MouseUp, AddressOf dtpacqudt_closeup
End Sub
Private Sub dtpacqudt_closeup(sender As Object, e As EventArgs) Handles DTPAcquDt.CloseUp
AddHandler DTPAcquDt.MouseUp, AddressOf dtpacqudt_closeup
'Check the Mouse/Keys event counter
If MyEventCounter > 0 Then
TxtDtAcqu.Text = DTPAcquDt.Value
'RESET The Counter
MyEventCounter = 0
End If
End Sub
Private Sub DTPAcquDt_KeyUp(sender As Object, e As KeyEventArgs) Handles DTPAcquDt.KeyUp
If e.KeyValue = Keys.Left OrElse e.KeyValue = Keys.Right Then
MyEventCounter = MyEventCounter + 1
End If
End Sub
Private Sub DTPAcquDt_MouseUp(sender As Object, e As MouseEventArgs) Handles DTPAcquDt.MouseUp
MyEventCounter = MyEventCounter + 1
End Sub
Private Sub DTPAcquDt_ValueChanged(sender As Object, e As EventArgs) Handles DTPAcquDt.ValueChanged
TxtDtAcqu.Text = DTPAcquDt.Value
'RESET The Counter
MyEventCounter = 0
End Sub
DTM_GETDATETIMEPICKERINFO
消息。它返回一个DATETIMEPICKERINFO
,其中应该包含底层编辑控件的句柄(以尝试发送EM_GETSEL
消息)。但是它没有返回它(为空)。然而,复选框和上下按钮(如果有)的位置被返回了。所以,你可以获取控件的屏幕截图...(是的,这很疯狂 :)) - Jimi