6

次のように指定されたフォームにDateTimePickerコントロールがあります。

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

ユーザーが時間を 5 分単位で増減できるようにしたいと思います。

これをどのように達成するかについて何か提案はありますか?

4

6 に答える 6

10

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;
    }
}
于 2009-12-04T16:17:33.830 に答える
7

Necromporph によって発見されたバグを排除するために、SixOThree からの回答を少し変更しました。次のように問題ないはずです。

クラスで

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;
}
于 2012-10-31T18:01:18.060 に答える
1

または、単にこれを試してください:

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);
}
于 2016-04-19T08:11:02.647 に答える
1

問題は、アップ/ダウン コントロールが、日付/時刻ピッカーの現在強調表示されている部分 (つまり、年/月/日/時間など) を自動的に増分または減分することです。

日付/時刻ピッカーのすぐ隣に独自のアップ/ダウン コントロール (おそらく非常に小さな vscrollbar) を追加し、日付/時刻ピッカーの値から 5 分間隔を増減するように配線することをお勧めします。

于 2009-12-04T10:22:52.617 に答える
0

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;
于 2011-09-28T18:44:28.260 に答える
0

これが古い記事であることは承知していますが、上記の回答に基づいて、この問題に対するより良い解決策を作成しました。

クラスで

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;
}
于 2011-08-02T21:18:24.807 に答える