Click here to Skip to main content
15,900,906 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
How do I control the up/down controls to only minutes in DateTimePicker?
Posted
Comments
Sergey Alexandrovich Kryukov 12-Nov-13 2:41am    
Not clear what do you want to achieve.
—SA

1 solution

As I see it there are three ways of achieving this.

1. Write your own TimePicker control where you dictate what can be changed. This is the hardest solution but the most complete as you get complete control of the changes.

2. Inherit DateTimePicker and override OnValueChanged in such a way that all changes that change something other that the minute are rejected.
Such an implementation might look something like this:
VB.NET
Public Class MyPicker
    Inherits DateTimePicker

    Private previous As DateTime

    Public Sub New()
        previous = Value
    End Sub

    Protected Overrides Sub OnValueChanged(eventargs As System.EventArgs)
        If previous.Minute = Value.Minute Then Value = previous

        previous = Value
    End Sub
End Class


3. Wire up the events on a normal DateTimePicker instance to do the logic described in solution 2.
That might look something like this:
VB
Public Class Form1

    Private previous As DateTime

    Public Sub New()
        InitializeComponent()

        previous = DateTimePicker1.Value
    End Sub

    Private Sub DateTimePicker1_ValueChanged(sender As System.Object, e As System.EventArgs) Handles DateTimePicker1.ValueChanged
        If previous.Minute = DateTimePicker1.Value.Minute Then DateTimePicker1.Value = previous

        previous = DateTimePicker1.Value
    End Sub

End Class


Note that solution 2 and 3 still allows the user to select other fields, it's jut that they are never allowed to be changed. This might feel un-intuitive to the user so I recommend going for solution 1.

Hope this helps,
Fredrik
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900