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 DTPs.
  • All DTPs have ShowCheckBoxes set to true AND Checked set on false.
  • When I run the program, the DTP on the first tab is fine, but when I check the DTP on the second tab, they show the current time, not the time that I set in the boot event.

Why is this happening? How can i avoid this?

+6
c # datetimepicker
source share
6 answers

My ugly workaround for this problem is to set the active tab that the DTP is in 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); 
+1
source share

I found out what the problem is.

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

Test case:

Does not 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; 

Working:

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

It seems to have nothing to do with the software add-in collector.

+3
source share

This is due to the checked datetimepicker property. It is usually set to false. At least that was a problem for me.

After setting the datetimepicker.checked parameter to true, it extracted the value from the settings.

+3
source share

DateTimePicker has some problems storing and retrieving its value. I had problems binding the value to an undetectable DateTime - from time to time I got NullReferenceExceptions. I don’t know why and when. Sometimes this happened and crashed the application.

0
source share

I just came across the same question using two DateTimePickers. I was able to get both of them to show the correct value, dynamically generating them and adding them to the form.

0
source share

If you cannot make it work, you can always try using a different collector. Any + Time (TM) Datepicker / Timepicker AJAX Calendar Widget extracts its value from the associated field, so if you initialize the field with a value, or change the field to have a value (for example, when loading), and then what you get when the collector is displayed. And if you have problems with this, just send a message to the contact page and it will be reviewed as soon as possible.

0
source share

All Articles