Question

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?

Was it helpful?

Solution 3

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

OTHER TIPS

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.

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.

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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top