Search timer control with half an hour up / down

I am looking for a timing solution that allows me to choose the following:

00:30 > 01:00 > 01:30 

When it reaches 23:30, it needs to be wrapped until 0:00.

In other words, I need to increase half an hour, picking up or down. I tried turning on the hscroll panel and making changes to the timepicker , but this is pretty sensitive and not necessary in my opinion, as I suspect there should be an easier way?

Any suggestions would be great.

+7
source share
4 answers

I just subclassed the DomainUpDown control to do this, here is 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 (ie if the user gets to 23:30 it wraps back around to 00:00 and vice versa) } } 

Add the control to your form as follows:

 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 (here I use the button), we just 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

+6
source

What I did in the past was created by a list (2 columns) and linked to combobox. The first columns were just a string representing the time ... the second value was double. Then, combboox, I will 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 has been a while since then, but can be generated using a simple loop in increments of .5, going from 0 to 23.50 (23:30 nights)

+1
source

One way I can come up with is to use a custom control with a ListBox with a built-in height set to one item and a separate vertical scroll bar located on top of the built-in list scroll bar.

Fill the ListBox with possible values, for example. 00:00 to 23:30 .

In the Scroll bar event, use a comparison of e.OldValue and e.NewValue to increase or decrease the TopIndex property in the ListBox so that the corresponding item is displayed and it seems to scroll up or down.

Then you can check if the first or last element is displayed, but since the scroll bar does not register scrolling in this event, you must transfer one or more elements to the first or last element so that it looks like the final one and the scollbar is always active and triggers an event Scroll

+1
source

Or you could make a simple decision in which you have one choice of time, which selects a date, and next to it is a ComboBox, which has a time of 00.00, 00.30 ... 23.00, 23.30. I am considering a solution in which the date picker will have the exact function you are looking for. Will edit this post if I find anything better.

EDIT:

You do not know which versions of .net have the NumericUpDown component, but if you have this, you can use this event with a modified value. Here is a sample code:

  decimal previousValue = 0; DateTime time = new DateTime(2000, 6, 10, 0, 0, 0); bool justChanged = false; private void numericUpDown1_ValueChanged(object sender, EventArgs e) { if (justChanged) { justChanged = !justChanged; return; } else justChanged = !justChanged; if (numericUpDown1.Value < previousValue) time = time.AddMinutes(-30); else time = time.AddMinutes(30); numericUpDown1.Value = decimal.Parse(time.ToString("HH,mm")); previousValue = numericUpDown1.Value; } 
0
source

All Articles