【问题标题】:Date Time Picker Manipulation日期时间选择器操作
【发布时间】:2012-02-18 08:26:00
【问题描述】:

我有 2 个 dateTimePicker 和 1 个 numericUpDown 值。

我想将 dateTimePicker1 设置为我的开始日期。并且每当我增加 numericUpdown 的值时,它都会从 dateTimepicker1 中的选定日期增加 1 天;

例如。 dateTimePicker1.Value =(2012 年 2 月 18 日 - 用户选择) dateTimePicker2.Value(2012 年 2 月 18 日)

所以如果用户没有增加 numericUpDownValue,DTP2 将等于 DTP1

因此,如果用户增加 numericUpdownValue(例如 1,则 DTP 将为(2012 年 2 月 19 日));

这是我尝试过的:

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            this.dateTimePicker1.MinDate = DateTime.Today.AddDays(1);
            this.dateTimePicker2.Value = dateTimePicker1.Value;

        }

 private void numericUpDown1_ValueChanged_1(object sender, EventArgs e)
        {

            int counter = Convert.ToInt16(numericUpDown1.Value + 1);
            this.dateTimePicker2.Value = DateTime.Now.AddDays(counter);
        }

我将 dateTimePicker 的值设置为今天提前 1 天。

此代码给我一个错误。是的,我知道这是不正确的,但至少我已经尝试过了。有愿意帮忙的吗?谢谢!

【问题讨论】:

  • "此代码给我一个错误。" ——什么错误?编译器错误或运行时错误?消息说了什么?
  • 逻辑错误? datetimepicker2 不是从 datetimepicker1 的日期开始的,我不知道如何在 dtp 上添加一天

标签: c# datetime datepicker datetimepicker numericupdown


【解决方案1】:

我试过这样,它对我来说很完美!!

看看下面的代码:

    private void numericUpDown1_ValueChanged(object sender, EventArgs e)
    {
        dateTimePicker2.Value = dateTimePicker2.Value.AddDays(Convert.ToInt32(numericUpDown1.Value));

    }

【讨论】:

  • 先生,您的代码有错误。 (例如:numericUpDown.value = 1)它增加了 (1 + 1 + 1...) 天。但是,如果我再次单击 NumericUpdown(值 = 2),它会增加两天!所以我添加的总天数是 3(必须是 2)。它不加 1。它加 (1 + 2 + 3....),所以如果我在 numericUpdown 中单击 3 次,它会加(6 天,必须只有 3 天)。此外,每当我减少 numericupdown 的值时,它都不会减去。 xD
猜你喜欢
  • 1970-01-01
  • 2012-09-11
  • 1970-01-01
  • 1970-01-01
  • 2015-04-30
  • 1970-01-01
  • 1970-01-01
  • 2018-11-23
相关资源
最近更新 更多