Posts

Showing posts from 2019

My Nullable DateTime Picker

Image
I needed to bind a nullable DateTime to a Winforms DateTimePicker control. Unfortunately, the standard DateTimePicker only supports regular DateTime objects. I looked around and found some controls that other people had built, but none of them did exactly what I needed.  I wanted a control where, if the checkbox is unchecked, the value is considered null and the box is blanked out. I couldn’t find one like that, so I built my own. The first one I built was written in VB.Net. In this blog post, we'll create a C# version.  We start by creating a new class and having it inherit from DateTimePicker.  using System.Windows.Forms; namespace NullableSample { public class NullableDateTimePicker : DateTimePicker { } } Then add a nullable DateTime property to hold the binded value.  public class NullableDateTimePicker : DateTimePicker { public DateTime? BindedValue { get; set; } } In the constructor, we set the ShowCheckBox value to true because we want it