简体   繁体   中英

Set DateTimePicker null in C#

So I have a DateTimePicker and I want to set it to null when save it into a database, but by default it has the today's value. If I want to remove it and empty the DateTimePicker I am not able too.

I've tried to put it into the load form:

dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = " ";

It actually works, but if I want to set a date it will not display into the DateTimePicker . Do you have any idea how can I solve it?

I have once written a date time picker that accepts null values (and allows week numbers too, as a free bonus).

[DefaultEvent("ValueChanged")]
[ComVisible(true)]
[DefaultProperty("Value")]
[DefaultBindingProperty("Value")]
[ClassInterface(ClassInterfaceType.AutoDispatch)]
[Designer("System.Windows.Forms.Design.DateTimePickerDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public class NullableDateTimePicker : DateTimePicker
{
    #region Externals
    private const int GWL_STYLE = (-16);
    private const int MCM_FIRST = 0x1000;
    private const int MCM_GETMINREQRECT = (MCM_FIRST + 9);
    private const int MCS_WEEKNUMBERS = 0x4;
    private const int DTM_FIRST = 0x1000;
    private const int DTM_GETMONTHCAL = (DTM_FIRST + 8);

    [DllImport("User32.dll")]
    public static extern int GetWindowLong(IntPtr h, int index);

    [DllImport("User32.dll")]
    public static extern int SetWindowLong(IntPtr h, int index, int value);

    [DllImport("User32.dll")]
    private static extern IntPtr SendMessage(IntPtr h, int msg, int param, int data);

    [DllImport("User32.dll")]
    private static extern int SendMessage(IntPtr h, int msg, int param, ref Rectangle data);

    [DllImport("User32.dll")]
    private static extern int MoveWindow(IntPtr h, int x, int y, int width, int height, bool repaint);

    [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
    private static extern IntPtr GetParent(IntPtr hWnd);
    #endregion

    #region General
    public NullableDateTimePicker()
    {
        this.ShowCheckBox = true;
    }
    #endregion

    #region Properties
    /// <summary>
    /// Gets or sets the date/time value assigned to the control.
    /// </summary>
    /// <exception cref="System.ArgumentOutOfRangeException">
    /// The set value is less than System.Windows.Forms.DateTimePicker.MinDate or more than System.Windows.Forms.DateTimePicker.MaxDate.
    /// </exception>
    [RefreshProperties(RefreshProperties.All)]
    [Bindable(true)]
    public new DateTime? Value
    {
        get
        {
            if (!base.Checked)
            {
                return null;
            }

            return base.Value;
        }
        set
        {
            if (value.HasValue)
            {
                base.Checked = true;

                if (this.Format == DateTimePickerFormat.Short)
                {
                    base.Value = value.Value.Date;
                }
                else if (this.Format == DateTimePickerFormat.Time)
                {
                    base.Value = default(DateTime).Add(value.Value.TimeOfDay);
                }
                else
                {
                    base.Value = value.Value;
                }
            }
            else
            {
                base.Checked = false;
            }
        }
    }

    /// <summary>
    /// Gets or sets whether to show week numbers.
    /// </summary>
    [Browsable(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    [DefaultValue(false)]
    public bool ShowWeekNumbers
    {
        get;
        set;
    }
    #endregion

    #region Week numbers
    /// <summary>
    /// Raises the System.Windows.Forms.DateTimePicker.DropDown event.
    /// </summary>
    /// <param name="e">An System.EventArgs that contains the event data.</param>
    protected override void OnDropDown(EventArgs e)
    {
        IntPtr monthView = SendMessage(this.Handle, DTM_GETMONTHCAL, 0, 0);
        int style = GetWindowLong(monthView, GWL_STYLE);

        if (this.ShowWeekNumbers)
        {
            style = style | MCS_WEEKNUMBERS;
        }
        else
        {
            style = style & ~MCS_WEEKNUMBERS;
        }

        Rectangle rect = new Rectangle();
        SetWindowLong(monthView, GWL_STYLE, style);
        SendMessage(monthView, MCM_GETMINREQRECT, 0, ref rect);
        MoveWindow(monthView, 0, 0, rect.Right + 3, rect.Bottom, true);

        //
        // Resize the surrounding window to let the new text fit
        //
        IntPtr parent = GetParent(monthView);

        Rectangle mainRect = new Rectangle();
        SendMessage(parent, MCM_GETMINREQRECT, 0, ref mainRect);
        MoveWindow(parent, 0, 0, mainRect.Right + 6, mainRect.Bottom + 6, true);

        base.OnDropDown(e);
    }
    #endregion
}

It shows a checkbox to allow null values, and the new Value property allows null too, so it works from both the designer and code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM