Search code examples
c#datetimepicker

how can I change the datetimepicker value(when it is readonly)


I'm trying to change the default value of a datetimepicker in my form in the formload event(I mean I don't want the datetimepicker's value to be 'now' when my app starts.I want it to be equal to some string value that my app should read from registry.) so I tried this:

    datepicker.Value.Date = //some string value converted to datetime

but it gets me an error that the datepicker is readonly and cannot be assigned. I can use the add method as my last solution.But I am hoping for a better one. thanks in advance.best regards.


Solution

  • You can set the Value. Value.Date however has no setter.

    private void Form1_Load(object sender, EventArgs e)
    {
         // this works
         dateTimePicker1.Value = dateTimePicker1.MinDate;
    
         // this does not
         dateTimePicker1.Value.Date = // Value.Date has no setter
    }