Results 1 to 3 of 3

Thread: [2005] DateTimePicker Up/Down Arrow Problem

  1. #1

    Thread Starter
    Hyperactive Member Jonny1409's Avatar
    Join Date
    Mar 2005
    Posts
    308

    [2005] DateTimePicker Up/Down Arrow Problem

    Hello,

    I have a datetimepicker on my form which is formatted to show time (HH:mm)
    This works a treat, but what I'd really like to do is have the minutes increment in blocks of 15 instead of the usual 1.

    Is this possible ?
    If so, how ?

    Thanks.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] DateTimePicker Up/Down Arrow Problem

    There's no way to do that with a regular DTP. You'd have to inherit the control and then implement the functionality yourself, although I'm not 100% sure how. You could override the WndProc method and work with its Windows messages perhaps. You may also be able to access the internal spinner control via its Controls collection. That may enable you to handle its events and determine when the value is to be incremented or decremented. This is all just conjecture though.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: [2005] DateTimePicker Up/Down Arrow Problem

    If you disallow the user from entering a time manually (this action is inhibited in the in code below), this gets you most of the way there...

    Code:
    Option Strict On
    Option Strict On
    Public Class Form1
    
        Dim ReferenceClock As Date
    
        Private Sub Form1_Load( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles MyBase.Load
    
            'Set DateTimePicker Format Hour:Minutes (24 hour with no Seconds field)
            DT.Format = DateTimePickerFormat.Custom
            DT.CustomFormat = "HH:mm"
            DT.ShowUpDown = False
    
            'Initialize the DateTimePicker to today
            Date.TryParse(Date.Today.ToString("dd/MM/yyyy") & " 00:00:00", ReferenceClock)
            DT.Value = ReferenceClock
    
        End Sub
    
        Private Sub DT_KeyDown( _
        ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.KeyEventArgs) _
        Handles DT.KeyDown
            'Note: We cannot discern between an hour or minute adjustment
            'so the up/down keys will have the same function whether in the
            'hour or minute field
            If e.KeyValue = Keys.Up Or e.KeyValue = Keys.Down Then
                'Increment
                If e.KeyValue = Keys.Up Then ReferenceClock = ReferenceClock.AddMinutes(15)
                'Decrement
                If e.KeyValue = Keys.Down Then ReferenceClock = ReferenceClock.AddMinutes(-15)
                'Update DateTimePicker
                DT.Value = ReferenceClock
            End If
        End Sub
    
        Private Sub DT_ValueChanged( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles DT.ValueChanged
            'Since the DateTimePicker will default to an increment/decrement of 1
            'this updates it again when the KeyDown event is completed
            'This also inhibits the user from manually specifying a time
            DT.Value = ReferenceClock
        End Sub
    
    End Class
    Last edited by Bulldog; Aug 15th, 2007 at 08:30 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width