Question

I am building a form with winforms - and should add a view and editable time span value. Which controls works best for this ?

A normal edit control shows the value correctly -> but it's not really nice for changing values

the normal date time picker doesn't work

the masked edit control doesn't have got any standard mask for this

Does anybody have a good idea for a control for this task ?

Was it helpful?

Solution 4

For another project I had to edit a time span too.

I've finally used 3 numeric up / down controls: the first controls the total hours, the second the minutes and the third the seconds.

//Setting the values
durationHours.Value = (Decimal)Math.Floor (data.TotalHours);
durationMin.Value = data.Minutes;
durationSeconds.Value = data.Seconds;

//Getting the values
data = new TimeSpan((int)durationHours.Value, (int)durationMin.Value, (int)durationSeconds.Value);

OTHER TIPS

I built one that had a TextBox coupled with a ComboBox to choose the units. The user control had range settings (value +-) and (time units). Based on the units picked, the text box was ranged checked - e.g., 1-7 days might be valid but if units were minutes, 1-60 minutes might be better.

I've done combinations of two ComboBoxes a NumericUpDown as well.

If you need time spans that are things like 3 days 4 hours 6 minutes, I'd probably opt for a user control with masked text box and range check the parts.

Typically, I opt for the first one, though.

Just my two cents.

Use the TimeSpan Helper, which includes a simple Control (that wasn't documented too well when I used it).

Use a normal textbox, coupled with an error provider control that checks the value using TimeSpan.TryParse() and a tooltip for suggesting what kind of data you're expecting.

If you want, you can combine all that into a single custom control, as well.

If your looking for some working code look at the link below.

This blog post provides source code for a WPF User Control for selecting a time span.

Also try this extended TimeSpanPicker

If you don't need want to allow more than 24 hours and you don't want to specify milliseconds I think the DateTime picker works the best, especially if you create a user control to make it a TimeSpan picker.

The nice thing is that you can updown the hours / minutes / seconds. It automatically prevents you to set a time of more then 60 minutes / seconds. The disadvantage: no milliseconds and no times longer than a day.

In developer studio add a new user control: TimePicker. On the user control put a DateTime picker. Change the properties:

  • Format: Time; this makes that you only see a time (with seconds) until 24 hours
  • ShowUpDown: True: this makes that you can't open a calendar. You'll have an up-down for the hours / minutes / seconds.

The localized time format is used. If you want a specific time format use the custom time format.

React on the event validated to read the value as a DateTime and subtract if from the Midnight at the beginning of the same day. The result will be a TimeSpan. Return this value in Valug.Get. Value.Set: add the DateTime of today midnight to the TimeSpan to set.

If you need more than 24 hours, or want to add milliseconds, add a textbox and use validating and TryParse. This will even allow users to used ticks instead of a time notitaion. If not validated, use a tool tip to display the error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top