Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTimePicker Default value: How to avoid it?

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?

like image 653
Jhonny D. Cano -Leftware- Avatar asked Sep 09 '09 15:09

Jhonny D. Cano -Leftware-


1 Answers

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.

like image 151
GONeale Avatar answered Oct 16 '22 19:10

GONeale