locked
Date Time Picker increments RRS feed

  • Question

  • Is there a way to set the incrementation of a date time picker so that it jumps 15 min with each change?

    I am using VB2010 express

    Thanks!


    Randy

    Wednesday, December 5, 2012 5:36 PM

Answers

  • Hi Randy,

    Here are some suggestions:

    1. Setting the MaxDate and the MinDate needs to be added into the Form load event. It only needs to be set once.

    2. When the time out of range, you need to add code to check and show the exception, here is a sample for keydown:

          If e.KeyCode = Keys.Down Then
                    Dim Checkdate As DateTime = DateTimePicker1.Value.AddMinutes(-15)
                    If Checkdate > DateTimePicker1.MaxDate Or Checkdate < DateTimePicker1.MinDate Then
                        MessageBox.Show(Checkdate.ToString & " is out of range")
                    Else
                        DateTimePicker1.Value = Checkdate
                    End If
                    e.Handled = True
                    End If

    By the way, please open a new thread for the issue has less relationship with the old one. It will benefit for the other members who want to find solutions quickly to similar issues.

    Have a nice day.


    Mark Liu-lxf
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, December 11, 2012 2:53 AM
    Moderator

All replies

  • Hi Randy,

    Here is a sample made for you, you can test it by add a Timer and a DateTimePicker to the form (the sample will add one day to DateTimePicker every 10 second):

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            DateTimePicker1.Value = DateTime.Today
            'tick event will occur every 1 second
            Timer1.Interval = 1000
            Timer1.Start()
        End Sub
        Dim break As Integer = 0
        Private Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
            break += 1
            '10 second add one day to DateTimePicker1
            If (break = 10) Then
                DateTimePicker1.Value = DateTimePicker1.Value.AddDays(1)
                break = 0
            End If
        End Sub

    Hope this helps.


    Mark Liu-lxf
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Giftzwockel Saturday, December 8, 2012 2:01 PM
    • Unproposed as answer by Randy042052 Monday, December 10, 2012 7:38 PM
    Thursday, December 6, 2012 6:24 AM
    Moderator
  • Mark,,

    thank you,, sorry it took so long to get back on here,,

    I dont think I explained my  question correctly

    What I am wanting to do is this,

    I have a dtp on my form,, it is set to diaplay time.

    what I would like to know is if it is possible to have the dtp increment a certain amount of time each time the user clicks on the up down arrows..

    Like,, the dtp shows 7:00

    the user clicks the up arrow once.

    the dtp show 7:15

    and increments up or down 15min each time it is clicked.

    is this possible? I have looked and looked but cannot find anything on the subject.

    Thanks

    Randy


    Randy

    Monday, December 10, 2012 3:13 AM
  • Hi Randy,

    Here is a sample made for you, it will meet your requirement, have a try:

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            KeyPreview = True
        End Sub
        Private Sub DateTimePicker1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles DateTimePicker1.KeyDown
            If e.KeyCode = Keys.Up Then
                DateTimePicker1.Value = DateTimePicker1.Value.AddMinutes(15)
                e.Handled = True
            End If
            If e.KeyCode = Keys.Down Then
                DateTimePicker1.Value = DateTimePicker1.Value.AddMinutes(-15)
                e.Handled = True
            End If
        End Sub

    Hope this helps.


    Mark Liu-lxf
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 10, 2012 4:55 AM
    Moderator
  • Mark,,

    Thanks! works great! Got one problem with it though,

    I have min and max set for the dtp and when the up or down key is held and the dtp goes over or under the min or max it errors. (see code below)

    I have looked and tried several things to limit or stop the keypress but cannot seem to find a way to stop the advance or retard when the limits are met.

    any ideas?

    Randy

    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            KeyPreview = True
            dtp1.Format = DateTimePickerFormat.Time
        End Sub
        Private Sub Dtp1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles dtp1.KeyDown
          
            If e.KeyCode = Keys.Up Then
                dtp1.Value = dtp1.Value.AddMinutes(15)
                e.Handled = True
            End If
            If e.KeyCode = Keys.Down Then
                dtp1.Value = dtp1.Value.AddMinutes(-15)
                e.Handled = True
            End If
           
        End Sub
        Private Sub dtp1_ValueChanged(sender As System.Object, e As System.EventArgs) Handles dtp1.ValueChanged
            dtp1.MinDate = Convert.ToDateTime(dtp1.Value.Date.ToShortDateString & " " & "07:00 AM")
            dtp1.MaxDate = Convert.ToDateTime(dtp1.Value.Date.ToShortDateString & " " & "03:00 PM")
        End Sub
    End Class


    Randy

    Monday, December 10, 2012 3:26 PM
  • Hi Randy,

    Here are some suggestions:

    1. Setting the MaxDate and the MinDate needs to be added into the Form load event. It only needs to be set once.

    2. When the time out of range, you need to add code to check and show the exception, here is a sample for keydown:

          If e.KeyCode = Keys.Down Then
                    Dim Checkdate As DateTime = DateTimePicker1.Value.AddMinutes(-15)
                    If Checkdate > DateTimePicker1.MaxDate Or Checkdate < DateTimePicker1.MinDate Then
                        MessageBox.Show(Checkdate.ToString & " is out of range")
                    Else
                        DateTimePicker1.Value = Checkdate
                    End If
                    e.Handled = True
                    End If

    By the way, please open a new thread for the issue has less relationship with the old one. It will benefit for the other members who want to find solutions quickly to similar issues.

    Have a nice day.


    Mark Liu-lxf
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, December 11, 2012 2:53 AM
    Moderator
  • Mark,,

    Thanks,, actually I figured out a way to make it work this afternoon,,not sure what you will think of it,, but here it is:

    Randy

    Private Sub Dtp1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles dtp1.KeyDown
            If e.KeyCode = Keys.Up And dtp1.Value < dtp1.MaxDate Then
                dtp1.Value = dtp1.Value.AddMinutes(10)
                e.Handled = True
            End If
            
            If e.KeyCode = Keys.Down And dtp1.Value > dtp1.MinDate Then
                dtp1.Value = dtp1.Value.AddMinutes(-10)
                e.Handled = True
            End If
        End Sub


    Randy


    • Edited by Randy042052 Tuesday, December 11, 2012 4:36 AM
    Tuesday, December 11, 2012 3:21 AM
  • Hi Randy,

    >>not sure what you will think of it.

    Just a quick scan of your code, it seems that the code will be failed in some specific point which leads to the exception.

    Here is a sample: when you press down key in 7:05, and the code will also works and get exception at last.

    You need to check the time after changed instead of before.

    Hope this helps.


    Mark Liu-lxf
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, December 11, 2012 4:59 AM
    Moderator