Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for time picker control with half hourly up/down

Tags:

c#

.net

winforms

I am looking for a solution to a time picker which allows me to select the following:

00:30  > 01:00  > 01:30

When it reaches 23:30 it needs to wrap around to 0:00.

In other words I need to increment a half hourly period by selecting up or down. I have tried incorporating a hscroll bar and amending a timepicker but this is quite sensitive and un-necessary in my view as I suspect there must be an easier way?

Any suggestions would be excellent.

like image 518
CR41G14 Avatar asked May 28 '13 10:05

CR41G14


2 Answers

I just sub-classed a DomainUpDown control to do this, here's the code:

class TimePicker : DomainUpDown
{
    public TimePicker()
    {         
        // build the list of times, in reverse order because the up/down buttons go the other way
        for (double time = 23.5; time >= 0; time -= 0.5)
        {
            int hour = (int)time; // cast to an int, we only get the whole number which is what we want
            int minutes = (int)((time - hour) * 60); // subtract the hour from the time variable to get the remainder of the hour, then multiply by 60 as .5 * 60 = 30 and 0 * 60 = 0

            this.Items.Add(hour.ToString("00") + ":" + minutes.ToString("00")); // format the hour and minutes to always have two digits and concatenate them together with the colon between them, then add to the Items collection
        }

        this.SelectedIndex = Items.IndexOf("09:00"); // select a default time

        this.Wrap = true; // this enables the picker to go to the first or last item if it is at the end of the list (i.e. if the user gets to 23:30 it wraps back around to 00:00 and vice versa)
    }
}

Add the control to your form like so:

TimePicker picker1;

public Form1()
{
    InitializeComponent();

    picker1 = new TimePicker();
    picker1.Name = "timePicker";
    picker1.Location = new Point(10, 10);

    Controls.Add(picker1);
}

Then when we want to get the selected time (I use a button here), we simply use the SelectedItem property:

private void button1_Click(object sender, EventArgs e)
{
    MessageBox.Show(picker1.SelectedItem.ToString()); // will show "09:00" when 09:00 is selected in the picker
}

Documentation for DomainUpDown: http://msdn.microsoft.com/en-us/library/system.windows.forms.domainupdown.aspx

like image 189
Sean Airey Avatar answered Oct 13 '22 12:10

Sean Airey


What I have done in the past was built a list (2 columns) and bound to the combobox. The first columns was just a string representing the time... the second was a double value corresponding to it. Then, the combboox, I would have the DISPLAY value based on the time representation, but the ACTUAL value based on the double value...

ex:

Display    Actual
00:00   =  0.0
00:30   =  0.5
01:00   =  1.0
....
12:30   =  12.5
13:00   =  13.0
etc.

It's been some time since that, but could be generated via a simple loop in increments of .5 going 0 to 23.50 (23:30 night)

like image 45
DRapp Avatar answered Oct 13 '22 11:10

DRapp