C# DateTimePicker类更改范围

C# DateTimePicker类更改范围,c#,C#,我需要将C#日期时间选择器控件设置为在范围内更改,我希望它只显示小时:分钟:秒,我希望最小值为10秒 例如 我该怎么做 您需要将onValueChanged事件添加到datePicker控件。下面是应该可以工作的代码。此代码用于win表单 public Form1() { InitializeComponent(); if (dt.Seconds % 10 > 0) { initialValue = true;

我需要将C#
日期时间选择器
控件设置为在范围内更改,我希望它只显示小时:分钟:秒,我希望最小值为10秒

例如


我该怎么做

您需要将onValueChanged事件添加到datePicker控件。下面是应该可以工作的代码。此代码用于win表单

public Form1()
{
    InitializeComponent();

    if (dt.Seconds % 10 > 0)
    {            
        initialValue = true;
        dateTimePicker1.Value = dt.Seconds (-(dt.Seconds % 10));
    }

    _prevDate = dateTimePicker1.Value;

}


private DateTime _prevDate;
private bool initialValue = false;

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
    if(initialValue)
    {
        initialValue = false;
        return;
    }

    DateTime dt = dateTimePicker1.Value;
    TimeSpan diff = dt - _prevDate;

    if (diff.Ticks < 0) 
        dateTimePicker1.Value = _prevDate.AddSeconds(-10);
    else 
        dateTimePicker1.Value = _prevDate.AddSeconds(10);

    _prevDate = dateTimePicker1.Value;
}
我玩了一会儿,下面是解决方案:
命名空间DataTimePicker1
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有无效dateTimePicker1\u值已更改(对象发送方,事件参数e)
{
DateTime dt=dateTimePicker1.Value;
如果(dt.Hour==0和&dt.Minute==0&&
dt.秒<10)
{
int inc=10-dt.秒;
dateTimePicker1.Value=dateTimePicker1.Value.AddSeconds(inc);
}
}
}
}

为什么?WPF?ASP.NET?WinForms?到目前为止,您是否尝试过任何可以发布的内容(即使它目前不起作用)?@Charlie不客气,祝您的项目或任务顺利!:)
public Form1()
{
    InitializeComponent();

    if (dt.Seconds % 10 > 0)
    {            
        initialValue = true;
        dateTimePicker1.Value = dt.Seconds (-(dt.Seconds % 10));
    }

    _prevDate = dateTimePicker1.Value;

}


private DateTime _prevDate;
private bool initialValue = false;

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
    if(initialValue)
    {
        initialValue = false;
        return;
    }

    DateTime dt = dateTimePicker1.Value;
    TimeSpan diff = dt - _prevDate;

    if (diff.Ticks < 0) 
        dateTimePicker1.Value = _prevDate.AddSeconds(-10);
    else 
        dateTimePicker1.Value = _prevDate.AddSeconds(10);

    _prevDate = dateTimePicker1.Value;
}
    this.dateTimePicker1.CustomFormat = "dd/MM/yyyy hh:mm:ss";
    this.dateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
    this.dateTimePicker1.ShowUpDown = true;
I played a while, here is the solution:

namespace DataTimePicker1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            DateTime dt = dateTimePicker1.Value;

            if (dt.Hour == 0 && dt.Minute == 0 &&
                dt.Second < 10)
            {
                int inc = 10 - dt.Second;
                dateTimePicker1.Value = dateTimePicker1.Value.AddSeconds(inc);
            }
        }
    }
}