How to Handle Key Events

RogerSchlueter-7899 1,236 Reputation points

I have a DatePicker on a wpf window. When the user presses the plus or minus keys I want to capture that event and increment the date value of the control by one day and increment the display accordingly.

Here is what I've tried:

Private Sub dtpChargeDate_PreviewKeyUp(sender As Object, e As KeyEventArgs) Handles dtpChargeDate.PreviewKeyUp
        If e.Key = Key.Subtract OrElse e.Key = Key.OemMinus OrElse e.Key = Key.Add OrElse e.Key = Key.OemPlus Then
            e.Handled = True
            <Change the date ....>
        End If

I thought that setting e.Handled = True would prevent the key from affecting the display but it doesn't. I end up with plus or minus characters in the textbox of the DatePicker which messes up the date value. How can I capture the key presses before they are processed normally?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,686 questions
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,602 questions
{count} votes

3 answers

Sort by: Most helpful
  1. LesHay 7,126 Reputation points

    The DateTimePicker automatically responds to the arrow keys when a field is focused in the DateTimePicker text area, and increases/decreases the field accordingly - day month year.
    I know you are asking for the +/- keys to do this, but seems overkill. Of course, you can do it that way too.

  2. Castorix31 82,036 Reputation points

    You can use the method at How to: Create a RoutedCommand :

                    <CommandBinding Command="{x:Static local:MainWindow.CustomRoutedCommand}"  
                        CanExecute="CanExecuteCustomCommand" />  

    when I test with Key.Subtract (added with CustomRoutedCommand.InputGestures.Add), the minus character is not displayed :


    0 comments No comments

  3. Hui Liu-MSFT 41,146 Reputation points Microsoft Vendor

    If you want to achieve previous/next day by -/+ keys, you could refer to the code below. If I misunderstood, please let me know.

            <DatePicker Name="dpick" Width="200" Height="40" PreviewKeyDown="dpick_PreviewKeyDown" />  


     Private Sub dpick_PreviewKeyDown(sender As Object, e As KeyEventArgs)  
            Dim dp = TryCast(sender, DatePicker)  
            If dp Is Nothing Then Return  
            If Not dp.SelectedDate.HasValue Then Return  
            Dim dayTime = dp.SelectedDate.Value  
            Select Case e.Key  
                Case Key.Add  
                    e.Handled = True  
                    MessageBox.Show("+ pressed")  
                    dp.SetValue(DatePicker.SelectedDateProperty, dayTime.AddDays(1))  
                Case Key.Subtract  
                    e.Handled = True  
                    MessageBox.Show("- pressed")  
                    dp.SetValue(DatePicker.SelectedDateProperty, dayTime.AddDays(-1))  
            End Select  
        End Sub  

    The result:

    If the response is helpful, please click "Accept Answer" and upvote it.
     Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread.


    0 comments No comments