寻找时间选择控制与半小时上升/下降

本文关键字:下降 半小时 时间 选择 控制 寻找 | 更新日期: 2023-09-27 18:15:45

我正在寻找一个时间选择器的解决方案,它允许我选择以下内容:

00:30  > 01:00  > 01:30

当它到达23:30时,它需要绕到0:00。

换句话说,我需要通过选择向上或向下来增加半小时周期。我曾尝试合并hscroll条并修改timepicker条,但在我看来,这是相当敏感和不必要的,因为我怀疑一定有更简单的方法?

寻找时间选择控制与半小时上升/下降

我只是子类化了一个DomainUpDown控件来做到这一点,下面是代码:

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)
    }
}

将控件添加到表单中,如下所示:

TimePicker picker1;
public Form1()
{
    InitializeComponent();
    picker1 = new TimePicker();
    picker1.Name = "timePicker";
    picker1.Location = new Point(10, 10);
    Controls.Add(picker1);
}

然后,当我们想要获得选择的时间(我在这里使用一个按钮),我们只需使用SelectedItem属性:

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
}

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

我过去所做的是构建一个列表(2列)并绑定到组合框。第一列只是一个表示时间的字符串…第二个是与它相对应的双精度值。然后,在combbox中,我将有基于时间表示的DISPLAY值,但实际值基于双精度值…

,

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

从那以后已经有一段时间了,但是可以通过一个简单的循环来生成,增量为0.5,从0到23.50(23:30晚上)

我能想到的一种方法是使用一个用户控件,将ListBox的整体高度设置为一个项目,并将一个单独的垂直滚动条放置在集成的ListBox滚动条的顶部。

用可能的值填充列表框,例如00:0023:30

在滚动条的滚动事件中,使用e.OldValuee.NewValue的比较来增加或减少ListBox的TopIndex属性,以便显示适当的项目,并且看起来是向上或向下滚动。

然后您可以检查是否显示了第一项或最后一项,但由于滚动条不会在该事件中注册滚动,因此您应该将一个或多个项目移动到第一项或最后一项之上,以便它看起来保持环绕,并且滚动条始终处于活动状态并引发其滚动事件

或者你可以做一个简单的解决方案,你有一个日期-时间选择器,只选择日期,旁边是一个组合框,有时间00.00,00.30…23.00、23.30。我正在寻找一个解决方案,其中日期选择器将有你正在寻找的确切功能。如果我找到更好的,我会编辑这篇文章。

<标题>编辑:

不确定哪些版本的。net有NumericUpDown组件,但如果你有它,你可以使用它的值改变事件。下面是一些示例代码:

    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;
    }