Click here to Skip to main content
15,867,962 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In a Windows application form, I need to put a time picker. I think I may select between System.Windows.Forms.DateTimePicker or Telerik.WinControls.UI.RadDateTimePicker. It's not to be used to select a date. It's to be used a time in a 24-hours. But the time should be aligned in half-an-hour, like 00:00, 00:30, 01:00 and so on. It should not be out of this. Eg. 20:45 or 05:53 are not acceptable. Is there an option to limit such a control to these aligned values?

What I have tried:

it's not applicable. it's not a bug. it's a feature i need.
Posted
Updated 21-Jun-17 3:37am

1 solution

See the Remarks section at DateTimePicker Class (System.Windows.Forms)[^].

[edit]
Set the properties according to the remarks in the link above to show a simple up down time control, and add the following code to the ValueChanged event handler:
C#
DateTime dt = dateTimePicker1.Value;
int mins = dt.Minute;
if (mins == 1 || mins == 31)
{
    dt = dt.AddMinutes(29);
    dateTimePicker1.Value = dt;
}

That will increase the time by 30 minutes every time the up button is pressed. I leave you to add the code for the down button and the change of hour.
 
Share this answer
 
v2
Comments
ilostmyid2 21-Jun-17 9:51am    
i did. but it doesn't include what i asked. i need it to spin up and down in half-an-hour boundaries.
Richard MacCutchan 21-Jun-17 11:43am    
See my update above.
ilostmyid2 21-Jun-17 12:04pm    
thanks for the code. i wonder why my code didn't work:

dateTimePicker1.Value.AddMinutes(29);
Richard MacCutchan 21-Jun-17 14:45pm    
dateTimePicker1.Valueis a DateTime object, and AddMinutes returns a new DateTime object. So you must set the value of your DateTimePicker to the newly created object. As the great Charles Petzold says in one of his books, "no one said it should be easy", which is maybe the Microsoft Vision statement.
ilostmyid2 21-Jun-17 21:49pm    
oh ic
thx
is there any way not to allow invalid values entered manually? ie. without using spin control.

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