GeorgeL GeorgeL - 1 year ago 115
C# Question

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?


Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download