winforms 如何在DateTimePicker中控制时间间隔

bxpogfeg  于 2023-10-23  发布在  其他
关注(0)|答案(7)|浏览(135)

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

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

我想用户只能增加或减少5分钟增量的时间。
有什么建议可以让我们做到这一点吗?

uqcuzwp8

uqcuzwp81#

可以通过观察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;
    }
}
lx0bsm1f

lx0bsm1f2#

我对SixOThree的答案做了一点修改,以消除Necromporph发现的bug。应该是这样的:
班上

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

nfzehxib3#

或者简单地试试这个:

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

gjmwrych4#

问题是向上/向下控件会自动递增或递减日期/时间选择器的当前突出显示部分(即年/月/日/小时/等)。
您最好在日期/时间选择器旁边添加自己的向上/向下控件(可能是一个非常小的vscrollbar),并将其连接到日期/时间选择器值的递增/递减五分钟间隔。

carvr3hs

carvr3hs5#

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

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

iecba09b6#

你可以把这段代码

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

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

之前

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

thtygnil7#

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

相关问题