Winforms &TimeSpan - Which control works best?
Asked Answered
W

7

5

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 ?

Weed answered 19/2, 2009 at 15:53 Comment(0)
W
3

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);
Weed answered 10/1, 2014 at 7:40 Comment(0)
B
5

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.

Bloomsbury answered 19/2, 2009 at 16:14 Comment(2)
the third sounds pretty much like I think the control should workWeed
Good answer. I'm opting for #3 as well.Librarianship
H
5

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

Heterolecithal answered 8/4, 2010 at 22:27 Comment(0)
I
4

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.

Ilan answered 19/2, 2009 at 15:56 Comment(0)
W
3

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);
Weed answered 10/1, 2014 at 7:40 Comment(0)
R
1

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.

Ravelin answered 29/7, 2009 at 9:57 Comment(0)
M
1

Also try this extended TimeSpanPicker

Marylouisemaryly answered 5/7, 2012 at 1:46 Comment(1)
This is for WPF, not for WinForms.Exorable
P
0

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.

Plumbum answered 12/11, 2013 at 9:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.