【问题标题】:Looking for time picker control with half hourly up/down寻找半小时向上/向下的时间选择器控件
【发布时间】:2013-05-28 10:11:11
【问题描述】:

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

00:30  > 01:00  > 01:30

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

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

任何建议都会很好。

【问题讨论】:

  • 也许您可以为您正在使用的 UI 框架找到一些东西。那是什么,WPF、WinForms、ASP.NET、MVC/Razor? - 编辑:哇,这个问题有 4 个赞。为什么?我看到几乎为零的研究。那里有大量的 UI 框架。看看Telerik,或this question
  • 首先出现在我脑海中的是 Enumerable.Range,但我很想看看最终解决方案的实际样子。
  • 对不起@CodeCaster 查看编辑
  • 这很有趣。这不是主题,但我认为系统正在生成时间表。
  • 没有办法在时间选择器上设置增量并使用自定义格式吗?

标签: c# .net winforms


【解决方案1】:

我只是对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

【讨论】:

  • +1 for 循环有点眼痛,但很好的实现。
  • 这一行的错误 this.SelectedIndex = times.IndexOf("09:00")
  • 对不起,我把List<string> 拿出来了,但是漏了一行,没有测试。这次我已经编辑和测试过了,一切正常=]
【解决方案2】:

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

例如:

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)

【讨论】:

    【解决方案3】:

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

    用可能的值填充 ListBox,例如00:0023:30

    在滚动条的 Scroll 事件中,使用 e.OldValuee.NewValue 的比较来增加或减少 ListBox 的 TopIndex 属性,以便显示相应的项目,并且它似乎正在向上或向下滚动.

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

    【讨论】:

      【解决方案4】:

      或者你可以做一个简单的解决方案,你有一个日期时间选择器,它只选择日期,旁边有一个 ComboBox,它的时间为 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;
          }
      

      【讨论】:

      • 在这种情况下我不需要日期,只需要时间。我可以使用下拉菜单,看看会出现什么。谢谢
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多