views:

1017

answers:

5

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?

A: 

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.

kubal5003
A: 

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.

CACuzcatlan
+2  A: 

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.

GONeale
A: 

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);
Jhonny D. Cano -Leftware-
A: 

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.

Andrew M. Andrews III