How can I programmatically close the dropdown calendar of a datetimepicker or update the dropdown calendar to reflect the .Value property?

只愿长相守 提交于 2019-12-23 05:05:37

问题


HELP, Please?! The issue is because I have an old usercontrol that makes use of a datetimepicker control. If there is no date to be displayed in the textbox of the datetimepicker then the .Value property is set to DateTimePicker.MinimumDateTime. OnValueChanged will update the CustomFormat to " " if the .Value is DateTimePicker.MinimumDateTime. Otherwise, the CustomFormat is "yyy-MM-dd hh:mm:ss tt".

Problem ==> In the DropDown event I check for the minimum datetime. If the .Value is equal to that then I update the .Value to be DateTime.Now. When the dropdown calendar is shown the the calendar is set for 1753-01-01, while the textbox (.Value) shows DateTime.Now.

How do I get the calendar to show the date that corresponds to the .Value property that was updated in the DropDown event? Even if there were a way to 'cancel' the first DropDown event on of the DateTimePicker when the value is changed from DateTimePicker.MinimumDateTime to DateTime.Now I think that could work, because the 2nd time (and subsequent times) the drop-down calendar is displayed the calendar correctly matches the date displayed in the textbox (DateTimePicker.Value).

Here is the code for the events that I have wired up to the DateTimePicker in question :

    private void ValueDatetimePickerOnKeyUp(Object sender, KeyEventArgs e)
    {
        if (e.KeyCode != Keys.Delete && e.KeyCode != Keys.Back)
            return;
        var dp = (DateTimePicker)sender;
        if (dp == null)
            return;
        dp.Value = DateTimePicker.MinimumDateTime;
    }

    private void ValueDatetimePickerDropDown(Object sender, EventArgs e)
    {
        var dp = (DateTimePicker)sender;
        if (dp == null)
            return;
        if (dp.Value == DateTimePicker.MinimumDateTime)
            dp.Value = DateTime.Now;
    }

    private void ValueDatetimePickerValueChanged(Object sender, EventArgs e)
    {
        var dp = (DateTimePicker)sender;
        if (dp == null)
            return;
        dp.CustomFormat = dp.Value == DateTimePicker.MinimumDateTime ? " " : "yyyy-MM-dd hh:mm:ss tt";
    }

回答1:


I've had some time to figure this out. It's a bit hacky, but basically in the DropDown event handler of the datetimepicker set the ShowUpDown to true and then invoke the Closeup event handler to have ShowUpDown set back to false. This will close the dropdown calendar and force the user to open it again which will then have the correct date shown on the calendar instead of 1/1/1753. The OnKeyUp event handler just allows the user to blank out the textbox value of the datetimepicker if they hit the DEL or Backspace key.

    private void ValueDatetimePickerOnKeyUp(Object sender, KeyEventArgs e)
    {
        //if user presses backspace or delete key then clear the date/time
        if (e.KeyCode != Keys.Delete && e.KeyCode != Keys.Back)
            return;
        var dp = (DateTimePicker)sender;
        if (dp == null)
            return;
        dp.Value = DateTimePicker.MinimumDateTime;
    }

    private void ValueDatetimePickerCloseUp(Object sender, EventArgs e)
    {
        var dp = (DateTimePicker) sender;
        if(dp == null)
            return;
        dp.ShowUpDown = false;
    }

    private void ValueDatetimePickerDropDown(Object sender, EventArgs e)
    {
        var dp = (DateTimePicker)sender;
        if (dp == null)
            return;
        if (dp.Value == DateTimePicker.MinimumDateTime)
        {
            dp.Value = DateTime.Now;
            dp.ShowUpDown = true;
            Invoke((MethodInvoker) (() => ValueDatetimePickerCloseUp(dp, new EventArgs())));
        }
    }

    private void ValueDatetimePickerValueChanged(Object sender, EventArgs e)
    {
        var dp = (DateTimePicker)sender;
        if (dp == null)
            return;
        dp.CustomFormat = dp.Value == DateTimePicker.MinimumDateTime ? " " : "yyyy-MM-dd HH:mm:ss tt";
    }



回答2:


create your own control and add this:

#region IsInputKey(Keys keyData)
        protected override bool IsInputKey(Keys keyData)
        {
            if (keyData == Keys.Tab
                //|| keyData.Equals(Keys.Up)
                //|| keyData.Equals(Keys.Down)
                //|| keyData.Equals(Keys.Left)
                //|| keyData.Equals(Keys.Right)
                || keyData.Equals(Keys.Enter)
                || keyData.Equals(Keys.Escape)
                || keyData.Equals(Keys.Space)
                )
                return true;

            return base.IsInputKey(keyData);
        }
        #endregion

this will allow all keys that you want to pass on KeyDown Event. ;D hf!!! AND WORKS WITH ANOTHER CONTROLS TOO, like TextBox, DataGridView, etc.



来源:https://stackoverflow.com/questions/15014280/how-can-i-programmatically-close-the-dropdown-calendar-of-a-datetimepicker-or-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!