DateTimePicker Default value: How to avoid it?

巧了我就是萌 提交于 2020-01-12 18:45:55

问题


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?


回答1:


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.




回答2:


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.




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



回答4:


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.




回答5:


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.




回答6:


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.



来源:https://stackoverflow.com/questions/1400525/datetimepicker-default-value-how-to-avoid-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!