locked
date time picker RRS feed

  • Question

  • Dear all

    In my windows form application I am using one data time picker in a short format suppose date is 17-10-2012 when I enter date I want that cursor should move automatically to next on month field instead of pressing next arrow key what i have to do that it will shift to month and after entering month to year.

    application is in visual studio 2010 and in visual basic

    Sunil Bhagwat

    Wednesday, October 17, 2012 5:32 PM

Answers

  • Hello Sunil,

    you can use the DateTimePicker.KeyDown and DateTimePicker.KeyUp events to do this.
    If you have a day or month like 5 you can enter 05.
    In the example a SendKeys of the right arrow key is made when the day or month portion
    of the DateTimePickers.Text Property have changed.

    It seems that the .Text Property is only changed when really for example the day has changed. It is not enough to type 1. Then the .Text Property seems not to change. It only changes when the Control can see the Day has changed. When you type 01 or 15.
    This was what i see when i tested it in Visual Studio Express 2012 for Windows Desktop.

    But if we want to change a date like 16.02.2012 to 16.10.2012 whe have still to type
    16.10 and not 1610 because the day remains the same. No changing of the .Text  Property is to see. In Germany the date separator is "." Entering the point is like entering the right arrow. I think this is the same with "-".

    Public Class Form1
      ' dtp1 is a Windows Forms DateTimePicker Control added by the Designer.

      Private dtp1_TextOld As String = ""

      Private Sub dtp1_KeyDown(sender As Object, e As KeyEventArgs) Handles dtp1.KeyDown
         dtp1_TextOld = dtp1.Text
      End Sub

      Private Sub dtp1_KeyUp(sender As Object, e As KeyEventArgs) Handles dtp1.KeyUp
        Dim dtp1_DateOld As Date
        Dim dtp1_DateCurrent As Date

        If dtp1_TextOld <> dtp1.Text Then
          ' The DateTimePickers.Text is changed when a Day or Month or Year changed.

         If Date.TryParse(dtp1_TextOld, dtp1_DateOld) _
         And Date.TryParse(dtp1.Text, dtp1_DateCurrent) Then

            If dtp1_DateOld.Day <> dtp1_DateCurrent.Day _
            Or dtp1_DateOld.Month <> dtp1_DateCurrent.Month Then
              SendKeys.Send("{Right}")
            End If

         End If

        End If
      End Sub

    End Class




     
    • Edited by Markus222 Friday, October 19, 2012 10:15 PM
    • Marked as answer by Youen Zen Tuesday, October 30, 2012 7:25 AM
    • Unmarked as answer by Sunil Bhagwat Thursday, November 1, 2012 5:47 AM
    • Marked as answer by Sunil Bhagwat Friday, November 2, 2012 7:27 PM
    Friday, October 19, 2012 9:52 PM
  • Dear All

    by simply adding one more line before  SendKeys.Send({"Right"}) I got my own questions answer

    The Line is as underlined below which will bring focus again to date for other entry or any correction in dates automatically to its start position.

     
    Or dtp1_DateOld.Month <> dtp1_DateCurrent.Month _
    
    Or dtp1_DateOld.Year <> dtp1_DateCurrent.Year Then
              SendKeys.Send("{Right}")
            End If
    

    I am satisfied with Markus Schertler suggestion with further my invention

    Sunil Bhagwat

    • Marked as answer by Sunil Bhagwat Saturday, November 3, 2012 7:02 PM
    Saturday, November 3, 2012 7:02 PM

All replies

  • hi sunil

    what you want is difficult to accomplish for this reason: how does the application "know" when you are done?

    using your example of 17-10-2012, and starting with the day of 17.

    you type the 1 - now what? The application does not know if the day is supposed to be merely 1, or if the day contains 2 digits as in 17.


    “This forum post is my own opinion and does not necessarily reflect the opinion or view of Microsoft, its employees, or other MVPs.”

    Thursday, October 18, 2012 5:21 AM
  • trere are 2 digit as in example it is a date of birth date time picker.
    Thursday, October 18, 2012 1:49 PM
  • Sunil,

    The datetime picker is a kind of standardized control. It works everywhere the same, not even in your application. It an unchangeable block controlled deep in the Windows OS.


    Success
    Cor

    Thursday, October 18, 2012 3:59 PM
  • Dear cor

    thanks for the information. my question is if instead of selecting date month year if we manually put the values if we enter date suppose 17 then entry is over then selection should shift to month automatically insted of pressing next arrow key.

    Sunil

    Friday, October 19, 2012 4:09 AM
  • Hello Sunil,

    you can use the DateTimePicker.KeyDown and DateTimePicker.KeyUp events to do this.
    If you have a day or month like 5 you can enter 05.
    In the example a SendKeys of the right arrow key is made when the day or month portion
    of the DateTimePickers.Text Property have changed.

    It seems that the .Text Property is only changed when really for example the day has changed. It is not enough to type 1. Then the .Text Property seems not to change. It only changes when the Control can see the Day has changed. When you type 01 or 15.
    This was what i see when i tested it in Visual Studio Express 2012 for Windows Desktop.

    But if we want to change a date like 16.02.2012 to 16.10.2012 whe have still to type
    16.10 and not 1610 because the day remains the same. No changing of the .Text  Property is to see. In Germany the date separator is "." Entering the point is like entering the right arrow. I think this is the same with "-".

    Public Class Form1
      ' dtp1 is a Windows Forms DateTimePicker Control added by the Designer.

      Private dtp1_TextOld As String = ""

      Private Sub dtp1_KeyDown(sender As Object, e As KeyEventArgs) Handles dtp1.KeyDown
         dtp1_TextOld = dtp1.Text
      End Sub

      Private Sub dtp1_KeyUp(sender As Object, e As KeyEventArgs) Handles dtp1.KeyUp
        Dim dtp1_DateOld As Date
        Dim dtp1_DateCurrent As Date

        If dtp1_TextOld <> dtp1.Text Then
          ' The DateTimePickers.Text is changed when a Day or Month or Year changed.

         If Date.TryParse(dtp1_TextOld, dtp1_DateOld) _
         And Date.TryParse(dtp1.Text, dtp1_DateCurrent) Then

            If dtp1_DateOld.Day <> dtp1_DateCurrent.Day _
            Or dtp1_DateOld.Month <> dtp1_DateCurrent.Month Then
              SendKeys.Send("{Right}")
            End If

         End If

        End If
      End Sub

    End Class




     
    • Edited by Markus222 Friday, October 19, 2012 10:15 PM
    • Marked as answer by Youen Zen Tuesday, October 30, 2012 7:25 AM
    • Unmarked as answer by Sunil Bhagwat Thursday, November 1, 2012 5:47 AM
    • Marked as answer by Sunil Bhagwat Friday, November 2, 2012 7:27 PM
    Friday, October 19, 2012 9:52 PM
  • Dear Markus Schertler

    Thanks for your reply it is working but i am trying to use it in my application let me check how to fix it in to application but now i got the idea from your reply

    Sunil Bhagwat

    Thursday, November 1, 2012 5:46 AM
  • Dear Markus Schertler

    Thanks for your reply it is working but i am trying to use it in my application let me check how to fix it in to application but now i got the idea from your reply

    Sunil Bhagwat

    Then why have you unmarked the reply from Markus as answer, I think that I'm not the only one who sees this as a kind of abuse.

    Success
    Cor

    Thursday, November 1, 2012 7:59 AM
  • Dear Markus Schertler

    your codes are working fine with my application but after putting yyyy value selection should again come to date position  for  that what I have to do.

    Sunil Bhagwat

    Friday, November 2, 2012 7:27 PM
  • Dear All

    by simply adding one more line before  SendKeys.Send({"Right"}) I got my own questions answer

    The Line is as underlined below which will bring focus again to date for other entry or any correction in dates automatically to its start position.

     
    Or dtp1_DateOld.Month <> dtp1_DateCurrent.Month _
    
    Or dtp1_DateOld.Year <> dtp1_DateCurrent.Year Then
              SendKeys.Send("{Right}")
            End If
    

    I am satisfied with Markus Schertler suggestion with further my invention

    Sunil Bhagwat

    • Marked as answer by Sunil Bhagwat Saturday, November 3, 2012 7:02 PM
    Saturday, November 3, 2012 7:02 PM
  • Dear Markus Schertler

    if date is same in some times as dtp1_Dateold.Day =dtp1_DateCurrent.Day and some time dtp.DateOld.Moth = dtp1_DateCurrent.Month Then it is not working, what to do for that pl explain.

    even in the both case sendkeys should work to change the focus.

    Sunil Bhagwat


    • Edited by Sunil Bhagwat Friday, November 9, 2012 4:56 AM omited one word
    Thursday, November 8, 2012 8:47 PM