When you add a DateTimePicker control, you can select part of the control value (for example the month) and then use the up/down arrows to modify this part of the date/time value (for example, incrementing/decrementing the month).
What i would like to do is to allow the same thing with the mouse wheel. I tried to register on the event MouseWheel but I can't find a way to know which part of my date/time is currently selected, so I have no way to know if i should increment the time, the day, the month or the year.
Is there any way to do this ?
A possible, but not quite elegant solution would be something like :
private void dateTimePicker1_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Delta > 0)
{
System.Windows.Forms.SendKeys.Send("{UP}");
}
else
{
System.Windows.Forms.SendKeys.Send("{DOWN}");
}
}
this should definitely work in general case, but some corner cases might have unexpected results (like having KeyUp/KeyDown events overriden)
I've tried to figure this one out too. In the end, I ended up with this:
Private Sub dtpDateTimePicker_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Delta > 0 Then
SendKeys.Send("{Up}")
Else
SendKeys.Send("{Down}")
End If
End Sub
You can automatically add this handler to all your date pickers in your form and it's containers using this method. Just call it once in form_load:
Public Sub AttachDateTimeMouseWheels(ByRef ownerForm As Control)
Dim myControl As Control
For Each myControl In ownerForm.Controls
If TypeOf myControl Is DateTimePicker Then
AddHandler myControl.MouseWheel, AddressOf dtpDateTimePicker_MouseWheel
Else
If myControl.Controls.Count > 0 Then
AttachDateTimeMouseWheels(myControl)
End If
End If
Next
End Sub
Use sendmessage WM_KEYDOWN/WM_KEYUP
Private Sub DateTimePickerAlarm_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DateTimePickerAlarm.MouseWheel
If e.Delta > 0 Then
Native.User32.SendMessage(DateTimePickerAlarm.Handle, &H100, Keys.Up, &H1480001)
Native.User32.SendMessage(DateTimePickerAlarm.Handle, &H101, Keys.Up, &HC1480001)
Else
Native.User32.SendMessage(DateTimePickerAlarm.Handle, &H100, Keys.Down, &H1500001)
Native.User32.SendMessage(DateTimePickerAlarm.Handle, &H101, Keys.Down, &HC1500001)
End If
'msg=0x100 (WM_KEYDOWN) hwnd=0x???????? wparam=0x26 lparam=0x01480001 result=0x0
'msg=0x101 (WM_KEYUP) hwnd=0x???????? wparam=0x26 lparam=0xc1480001 result=0x0
'msg=0x100 (WM_KEYDOWN) hwnd=0x???????? wparam=0x28 lparam=0x01500001 result=0x0
'msg=0x101 (WM_KEYUP) hwnd=0x???????? wparam=0x28 lparam=0xc1500001 result=0x0
End Sub
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With