DateTimePicker Default value: How to avoid it?

2019-04-07 11:24发布

Facts:

  • I have a TabControl with 2 Tabs, Each tab has 2 DateTimePicker.
  • In the Load event, I set the value of all the DTPs.
  • All DTPs have ShowCheckBoxes set on true AND Checked set on false.
  • When I Execute the program, The DTPs in first tab are OK, but when I check the DTPs on second tab, they show current time, not the time I set on the load event.

Why this happen? How can I avoid it?

6条回答
萌系小妹纸
2楼-- · 2019-04-07 11:54

I just ran into the same issue using two DateTimePickers. I was able to get both of them to show the correct value by dynamically generating them and adding them to the form.

查看更多
Juvenile、少年°
3楼-- · 2019-04-07 12:04

My ugly workaround to this issue, consists on set active the tab in which the DTP's reside before changing its values, something like this:

DateTime dat1 = DateTime.Today;
DateTime dat2 = dat1.AddDays(1).AddSeconds(-1);

dtpCreatedStart.Value = dat1;
dtpCreatedEnd.Value = dat2;
tbc.SelectTab(1);
dtpModifiedStart.Value = dat1;
dtpModifiedEnd.Value = dat2;
tbc.SelectTab(0);
查看更多
再贱就再见
4楼-- · 2019-04-07 12:06

It has to do with the Checked property of the datetimepicker. It is usually set to false. At least that was the problem to me.

After setting the datetimepicker.checked to true, it did retrieve the value from settings.

查看更多
SAY GOODBYE
5楼-- · 2019-04-07 12:10

I found out what the problem was here.

The Value property only sets a new value if the DateTimePicker control is visible. Otherwise command is ignored.

Test case:

Doesn't work:

 this.picker = new DateTimePicker
        {
            Checked = false,
            Font = new System.Drawing.Font("Verdana", 9.75F),
            Format = System.Windows.Forms.DateTimePickerFormat.Time,
            Location = new System.Drawing.Point(5, 5),
            Name = "picker",
            ShowUpDown = true,
            Size = new System.Drawing.Size(120, 23),
            Visible = false
        };
        this.Controls.Add(this.picker);
        this.picker.Value = this.picker.Value.Date.AddHours(1);
        this.picker.Visible = true;

Works:

 this.picker = new DateTimePicker
        {
            Checked = false,
            Font = new System.Drawing.Font("Verdana", 9.75F),
            Format = System.Windows.Forms.DateTimePickerFormat.Time,
            Location = new System.Drawing.Point(5, 5),
            Name = "picker",
            ShowUpDown = true,
            Size = new System.Drawing.Size(120, 23),
            Visible = false
        };
        this.Controls.Add(this.picker);
        this.picker.Visible = true;
        this.picker.Value = this.picker.Value.Date.AddHours(1);

Doesn't appear to have anything to do with programatically adding the picker it seems.

查看更多
混吃等死
6楼-- · 2019-04-07 12:13

DateTimePicker has some issues with storing and retrieving it's value. I had trouble when binding the Value to a non nullable DateTime - I got NullReferenceExeptions from time to time. No idea why or when. It sometimes just happened and crashed the app.

查看更多
爷、活的狠高调
7楼-- · 2019-04-07 12:13

If you can't get it to work, you can always try using a different picker. The Any+Time(TM) Datepicker/Timepicker AJAX Calendar Widget pulls its value from the associated field, so if you initialize the field with a value, or change a field to have a value (such as in onload), then that's what you'll get when the picker is displayed. And if you have problems with it, just submit a message via the contact page, and it will be addressed ASAP.

查看更多
登录 后发表回答