Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a DateTimePicker increment/decrement on mouse wheel

Tags:

c#

winforms

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 ?

like image 232
Ksempac Avatar asked Jun 22 '09 09:06

Ksempac


3 Answers

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)

like image 51
Vorber Avatar answered Oct 02 '22 10:10

Vorber


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
like image 26
Jiri Avatar answered Oct 02 '22 11:10

Jiri


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
like image 22
Vozzie Avatar answered Oct 02 '22 10:10

Vozzie