Search code examples
c#winformsdatetimepicker

How to set the current time in a DateTimePicker


I am trying to set the current time to a DateTimePicker (with Format Time) like

this.myDateTimePicker.Value = DateTime.Now;

but when executing my code I am getting an exception

Object reference not set to an instance of an object    

What I am doing wrong?

Thanks.


Solution

  • You need to put that code after the InitializeComponent() call is made. There is no instance of myDateTimePicker until that point.