How to Handle Key Events

RogerSchlueter-7899 1,236 Reputation points
2022-02-27T03:48:11.523+00:00

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
VB
VB
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
    2022-02-27T09:08:52.937+00:00

    Hi
    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
    2022-02-27T09:22:01.46+00:00

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

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

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

    178080-customroutedcommand.gif

    0 comments No comments

  3. Hui Liu-MSFT 41,146 Reputation points Microsoft Vendor
    2022-02-28T06:46:59.267+00:00

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

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

    MainWIndow.xaml.vb:

     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:
    178362-2.gif


    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.
     

    [5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html

    0 comments No comments