How to set the current time in a DateTimePicker

georgeliatsos picture georgeliatsos · Aug 2, 2012 · Viewed 57.3k times · Source

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.

Answer

code4life picture code4life · Aug 2, 2012

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