Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 寻找半小时向上/向下的时间选择器控制_C#_.net_Winforms - Fatal编程技术网

C# 寻找半小时向上/向下的时间选择器控制

C# 寻找半小时向上/向下的时间选择器控制,c#,.net,winforms,C#,.net,Winforms,我正在寻找时间选择器的解决方案,它允许我选择以下选项: 00:30 > 01:00 > 01:30 当它到达23:30时,它需要绕到0:00 换句话说,我需要通过选择up或down来增加半小时周期。我曾尝试合并一个hscroll条并修改一个timepicker,但在我看来,这是非常敏感且不必要的,因为我怀疑一定有更简单的方法 任何建议都很好。或者你可以做一个简单的解决方案,你有一个只选择日期的日期时间选择器,旁边有一个组合框,上面有时间00.00,00.30。。。23.00,

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

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

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


任何建议都很好。

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

编辑: 不确定哪些版本的.net具有NumericUpDown组件,但如果确实具有该组件,则可以将value changed事件用于该组件。下面是一些示例代码:

    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;
    }
decimal-previousValue=0;
日期时间=新的日期时间(2000,6,10,0,0,0);
bool justChanged=false;
私有void numericUpDown1_值已更改(对象发送方,事件参数e)
{
如果(刚刚更改)
{
justChanged=!justChanged;
返回;
}
其他的
justChanged=!justChanged;
if(numericUpDown1.Value<先前的值)
时间=时间加分钟(-30);
其他的
时间=时间。添加分钟(30);
numericUpDown1.Value=decimal.Parse(time.ToString(“HH,mm”));
previousValue=numericUpDown1.值;
}

我过去所做的是构建一个列表(2列)并绑定到组合框。第一列只是一个表示时间的字符串。。。第二个是对应于它的双值。然后,comboox,我会有基于时间表示的显示值,但实际值基于双值

例:


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

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

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

在滚动条的滚动事件中,使用比较
e.OldValue
e.NewValue
来增加或减少列表框的
TopIndex
属性,以便显示相应的项目,并显示为向上或向下滚动


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

我刚刚将
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的文档

也许你可以找到一些你正在使用的UI框架的东西。那是什么,WPF,WinForms,ASP.NET,MVC/Razor编辑:哇,这个问题有4票赞成。为什么?我几乎看不到任何研究。有很多UI框架。看看,或者。我脑海中闪现的第一件事是可枚举的。范围,但我想看看最终的解决方案实际上是什么样子。抱歉@CodeCaster see editWell这很有趣。这与主题无关,但我认为系统正在生成时间表。无法在时间选择器上设置增量并使用自定义格式?在本例中,我不需要日期,只需要时间。我需要一个下拉列表,看看会出现什么。感谢+1
for
循环有点让人头疼,但实现得很好。这一行的错误是this.SelectedIndex=times.IndexOf(“09:00”)对不起,我取出了
列表,但漏掉了一行,没有进行测试。这次我已经编辑并测试过了,一切正常=]
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
}