如何控制 DateTimePicker 中的时间间隔

发布于 2024-08-13 16:02:33 字数 266 浏览 5 评论 0原文

我在指定的表单上有一个 DateTimePicker 控件,如下所示:

dtpEntry.Format = DateTimePickerFormat.Custom;
dtpEntry.CustomFormat = "dd/MM/yyyy hh:mm:ss";
dtpEntry.ShowUpDown = true;

我希望用户只能以 5 分钟的增量增加或减少时间。

关于如何实现这一目标有什么建议吗?

I have a DateTimePicker control on a form specified like so:

dtpEntry.Format = DateTimePickerFormat.Custom;
dtpEntry.CustomFormat = "dd/MM/yyyy hh:mm:ss";
dtpEntry.ShowUpDown = true;

I would like the user to only be able to increment or decrement the time by 5 minute increments.

Any suggestions on how one would accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

叹梦 2024-08-20 16:02:33

可以通过观察 ValueChanged 事件并覆盖该值来实现。这个样本表格效果很好:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        dateTimePicker1.CustomFormat = "dd/MM/yyyy hh:mm";
        dateTimePicker1.Format = DateTimePickerFormat.Custom;
        dateTimePicker1.ShowUpDown = true;
        dateTimePicker1.Value = DateTime.Now.Date.AddHours(DateTime.Now.Hour);
        mPrevDate = dateTimePicker1.Value;
        dateTimePicker1.ValueChanged += new EventHandler(dateTimePicker1_ValueChanged);
    }
    private DateTime mPrevDate;
    private bool mBusy;

    private void dateTimePicker1_ValueChanged(object sender, EventArgs e) {
        if (!mBusy) {
            mBusy = true;
            DateTime dt = dateTimePicker1.Value;
            if ((dt.Minute * 60 + dt.Second) % 300 != 0) {
                TimeSpan diff = dt - mPrevDate;
                if (diff.Ticks < 0) dateTimePicker1.Value = mPrevDate.AddMinutes(-5);
                else dateTimePicker1.Value = mPrevDate.AddMinutes(5);
            }
            mBusy = false;
        }
        mPrevDate = dateTimePicker1.Value;
    }
}

It's possible by watching the ValueChanged event and override the value. This sample form worked well:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        dateTimePicker1.CustomFormat = "dd/MM/yyyy hh:mm";
        dateTimePicker1.Format = DateTimePickerFormat.Custom;
        dateTimePicker1.ShowUpDown = true;
        dateTimePicker1.Value = DateTime.Now.Date.AddHours(DateTime.Now.Hour);
        mPrevDate = dateTimePicker1.Value;
        dateTimePicker1.ValueChanged += new EventHandler(dateTimePicker1_ValueChanged);
    }
    private DateTime mPrevDate;
    private bool mBusy;

    private void dateTimePicker1_ValueChanged(object sender, EventArgs e) {
        if (!mBusy) {
            mBusy = true;
            DateTime dt = dateTimePicker1.Value;
            if ((dt.Minute * 60 + dt.Second) % 300 != 0) {
                TimeSpan diff = dt - mPrevDate;
                if (diff.Ticks < 0) dateTimePicker1.Value = mPrevDate.AddMinutes(-5);
                else dateTimePicker1.Value = mPrevDate.AddMinutes(5);
            }
            mBusy = false;
        }
        mPrevDate = dateTimePicker1.Value;
    }
}
最后的乘客 2024-08-20 16:02:33

我对 SixOThree 的答案做了一些更改,以消除 Necromporph 发现的错误。
应该是这样的:

类中

private DateTime prevTimePicker1;
private bool navigatingDateTimePicker = false;

在构造函数的

prevTimePicker1 = dateTimePickerStart.Value;
navigatingDateTimePicker = false;

事件

private void dateTimePickerStart_ValueChanged(object sender, EventArgs e)
{
  if (!navigatingDateTimePicker) {
    /* First set the navigating flag to true so this method doesn't get called again while updating */
    navigatingDateTimePicker = true;

    /* using timespan because that's the only way I know how to round times well */
    TimeSpan tempTS = dateTimePickerStart.Value - dateTimePickerStart.Value.Date;
    TimeSpan roundedTimeSpan;

    TimeSpan TDBug = dateTimePickerStart.Value - prevTimePicker1;
    if (TDBug.TotalMinutes == 59)
    {
        // first: if we are going back and skipping an hour it needs an adjustment
        roundedTimeSpan = TimeSpan.FromMinutes(5 * Math.Floor((tempTS.TotalMinutes - 60) / 5));
        dateTimePickerStart.Value = dateTimePickerStart.Value.Date + roundedTimeSpan;
    }
    else if (dateTimePickerStart.Value > prevTimePicker1)
    {
        // round up to the nearest interval
        roundedTimeSpan = TimeSpan.FromMinutes(5 * Math.Ceiling(tempTS.TotalMinutes / 5));
        dateTimePickerStart.Value = dateTimePickerStart.Value.Date + roundedTimeSpan;
    } else {
        // round down to the nearest interval from prev
        roundedTimeSpan = TimeSpan.FromMinutes(5 * Math.Floor(tempTS.TotalMinutes / 5));
        dateTimePickerStart.Value = dateTimePickerStart.Value.Date + roundedTimeSpan;
    }
    navigatingDateTimePicker = false;
  }
  prevTimePicker1 = dateTimePickerStart.Value;
}

I've changed a little the answer from SixOThree, to eliminate the bug found by Necromporph.
It should be ok like this:

in the class

private DateTime prevTimePicker1;
private bool navigatingDateTimePicker = false;

in the constructor

prevTimePicker1 = dateTimePickerStart.Value;
navigatingDateTimePicker = false;

the event

private void dateTimePickerStart_ValueChanged(object sender, EventArgs e)
{
  if (!navigatingDateTimePicker) {
    /* First set the navigating flag to true so this method doesn't get called again while updating */
    navigatingDateTimePicker = true;

    /* using timespan because that's the only way I know how to round times well */
    TimeSpan tempTS = dateTimePickerStart.Value - dateTimePickerStart.Value.Date;
    TimeSpan roundedTimeSpan;

    TimeSpan TDBug = dateTimePickerStart.Value - prevTimePicker1;
    if (TDBug.TotalMinutes == 59)
    {
        // first: if we are going back and skipping an hour it needs an adjustment
        roundedTimeSpan = TimeSpan.FromMinutes(5 * Math.Floor((tempTS.TotalMinutes - 60) / 5));
        dateTimePickerStart.Value = dateTimePickerStart.Value.Date + roundedTimeSpan;
    }
    else if (dateTimePickerStart.Value > prevTimePicker1)
    {
        // round up to the nearest interval
        roundedTimeSpan = TimeSpan.FromMinutes(5 * Math.Ceiling(tempTS.TotalMinutes / 5));
        dateTimePickerStart.Value = dateTimePickerStart.Value.Date + roundedTimeSpan;
    } else {
        // round down to the nearest interval from prev
        roundedTimeSpan = TimeSpan.FromMinutes(5 * Math.Floor(tempTS.TotalMinutes / 5));
        dateTimePickerStart.Value = dateTimePickerStart.Value.Date + roundedTimeSpan;
    }
    navigatingDateTimePicker = false;
  }
  prevTimePicker1 = dateTimePickerStart.Value;
}
情独悲 2024-08-20 16:02:33

或者简单地尝试一下:

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
    if (this.dateTimePicker1.Value.Minute % 5 == 0)
        return;

    if (this.dateTimePicker1.Value.Minute % 5 == 1)
        this.dateTimePicker1.Value = this.dateTimePicker1.Value.AddMinutes(4);

    if (this.dateTimePicker1.Value.Minute % 5 == 4)
        this.dateTimePicker1.Value = this.dateTimePicker1.Value.AddMinutes(-4);
}

Or simply try this:

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
    if (this.dateTimePicker1.Value.Minute % 5 == 0)
        return;

    if (this.dateTimePicker1.Value.Minute % 5 == 1)
        this.dateTimePicker1.Value = this.dateTimePicker1.Value.AddMinutes(4);

    if (this.dateTimePicker1.Value.Minute % 5 == 4)
        this.dateTimePicker1.Value = this.dateTimePicker1.Value.AddMinutes(-4);
}
遥远的她 2024-08-20 16:02:33

问题在于,向上/向下控件会自动递增或递减日期/时间选择器当前突出显示的部分(即年/月/日/小时/等)。

您最好在紧邻日期/时间选择器的位置添加自己的向上/向下控件(可能是一个非常小的 vscrollbar),并将其连接起来以从日期/时间选择器的值增加/减少五分钟间隔。

The problem is that the up/down control automatically increments or decrements the currently highlighted portion of the date/time picker (i.e. year/month/day/hour/etc.).

You are probably better off adding your own up/down control (perhaps a very small vscrollbar) immediately adjacent to the date/time picker and wiring it up to increment/decrement five minute intervals from the date/time picker's value.

悟红尘 2024-08-20 16:02:33

我知道这是一篇旧文章,但我根据上面的答案创建了一个更好的解决方案来解决这个问题。

在类

private DateTime prevTimePicker1;
private bool navigatingDateTimePicker = false;

的构造函数中

prevTimePicker1 = dateTimePickerStart.Value;
navigatingDateTimePicker = false;

事件

private void dateTimePickerStart_ValueChanged(object sender, EventArgs e)
{
  if (!navigatingDateTimePicker) {
    /* First set the navigating flag to true so this method doesn't get called again while updating */
    navigatingDateTimePicker = true;

    /* using timespan because that's the only way I know how to round times well */
    TimeSpan tempTS = dateTimePickerStart.Value - dateTimePickerStart.Value.Date;
    TimeSpan roundedTimeSpan;

    if (dateTimePickerStart.Value > prevTimePicker1) {
      // round up to the nearest interval
      roundedTimeSpan = TimeSpan.FromMinutes(5 * Math.Ceiling(tempTS.TotalMinutes / 5));
      dateTimePickerStart.Value = dateTimePickerStart.Value.Date + roundedTimeSpan;
    } else {
      // round down to the nearest interval from prev
      roundedTimeSpan = TimeSpan.FromMinutes(5 * Math.Floor(tempTS.TotalMinutes / 5));
      dateTimePickerStart.Value = dateTimePickerStart.Value.Date + roundedTimeSpan;
    }
    navigatingDateTimePicker = false;
  }
  prevTimePicker1 = dateTimePickerStart.Value;
}

I know this is an old article, but I created a better solution to this problem based on the answer above.

in the class

private DateTime prevTimePicker1;
private bool navigatingDateTimePicker = false;

in the constructor

prevTimePicker1 = dateTimePickerStart.Value;
navigatingDateTimePicker = false;

the event

private void dateTimePickerStart_ValueChanged(object sender, EventArgs e)
{
  if (!navigatingDateTimePicker) {
    /* First set the navigating flag to true so this method doesn't get called again while updating */
    navigatingDateTimePicker = true;

    /* using timespan because that's the only way I know how to round times well */
    TimeSpan tempTS = dateTimePickerStart.Value - dateTimePickerStart.Value.Date;
    TimeSpan roundedTimeSpan;

    if (dateTimePickerStart.Value > prevTimePicker1) {
      // round up to the nearest interval
      roundedTimeSpan = TimeSpan.FromMinutes(5 * Math.Ceiling(tempTS.TotalMinutes / 5));
      dateTimePickerStart.Value = dateTimePickerStart.Value.Date + roundedTimeSpan;
    } else {
      // round down to the nearest interval from prev
      roundedTimeSpan = TimeSpan.FromMinutes(5 * Math.Floor(tempTS.TotalMinutes / 5));
      dateTimePickerStart.Value = dateTimePickerStart.Value.Date + roundedTimeSpan;
    }
    navigatingDateTimePicker = false;
  }
  prevTimePicker1 = dateTimePickerStart.Value;
}
美羊羊 2024-08-20 16:02:33

添加这段代码

int minuteDiff = dtpJamAppointmentDokter.Value.Minute - prevTimePicker1.Minute;

if (minuteDiff == 59)
{
    dtpJamAppointmentDokter.Value =  dtpJamAppointmentDokter.Value.AddHours(-1);
}

你可以之前

TimeSpan tempTS = dtpJamAppointmentDokter.Value - dtpJamAppointmentDokter.Value.Date;

you could add this code

int minuteDiff = dtpJamAppointmentDokter.Value.Minute - prevTimePicker1.Minute;

if (minuteDiff == 59)
{
    dtpJamAppointmentDokter.Value =  dtpJamAppointmentDokter.Value.AddHours(-1);
}

before

TimeSpan tempTS = dtpJamAppointmentDokter.Value - dtpJamAppointmentDokter.Value.Date;
北方的巷 2024-08-20 16:02:33

@Hans Passant 只需要处理 59 即可正常工作。

private void dateTimePickerReportScheduleTime_ValueChanged(object sender, EventArgs e)
     {
         if (!mBusy)
         {
             mBusy = true;
             DateTime dt = dateTimePickerReportScheduleTime.Value;
             if ((dt.Minute * 60 + dt.Second) % 300 != 0)
             {
                 TimeSpan diff = dt - mPrevDate;
                 if (dt.Minute == 59) 
                 { 
                     dateTimePickerReportScheduleTime.Value = mPrevDate.AddMinutes(-15); 
                 }
                 else
                 {
                     if (diff.Ticks < 0) dateTimePickerReportScheduleTime.Value = mPrevDate.AddMinutes(-15);
    
                     else dateTimePickerReportScheduleTime.Value = mPrevDate.AddMinutes(15);
                 }
             }
             mBusy = false;
         }
         mPrevDate = dateTimePickerReportScheduleTime.Value;
     }

@Hans Passant Just needed to handle 59 for it to work right.

private void dateTimePickerReportScheduleTime_ValueChanged(object sender, EventArgs e)
     {
         if (!mBusy)
         {
             mBusy = true;
             DateTime dt = dateTimePickerReportScheduleTime.Value;
             if ((dt.Minute * 60 + dt.Second) % 300 != 0)
             {
                 TimeSpan diff = dt - mPrevDate;
                 if (dt.Minute == 59) 
                 { 
                     dateTimePickerReportScheduleTime.Value = mPrevDate.AddMinutes(-15); 
                 }
                 else
                 {
                     if (diff.Ticks < 0) dateTimePickerReportScheduleTime.Value = mPrevDate.AddMinutes(-15);
    
                     else dateTimePickerReportScheduleTime.Value = mPrevDate.AddMinutes(15);
                 }
             }
             mBusy = false;
         }
         mPrevDate = dateTimePickerReportScheduleTime.Value;
     }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文