我怎么能编程方式关闭一个DateTimePicker的下拉日历或更新下拉日历来反映。价值的财产? [英] How can I programmatically close the dropdown calendar of a datetimepicker or update the dropdown calendar to reflect the .Value property?

查看:644
本文介绍了我怎么能编程方式关闭一个DateTimePicker的下拉日历或更新下拉日历来反映。价值的财产?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

帮助,请?问题是,因为我有一个旧的用户控件,它使用一个DateTimePicker控制。如果没有要显示在的DateTimePicker则。价值属性设置为DateTimePicker.MinimumDateTime的文本框日期。 OnValueChanged将更新CustomFormat为如果.value的是DateTimePicker.MinimumDateTime。否则,CustomFormat是YYY-MM-DD HH:MM:SS TT。

问题==>在下拉事件中,我检查的最小日期时间。如果.value的等于然后我更新。价值是DateTime.Now。当下拉日历显示的设置为1753年1月1日的日历,而文本框(.value的)显示DateTime.Now。

如何获得日历显示,对应到在下拉事件更新。价值财产的日期?即使有一种方法对我认为可以工作时的值从DateTimePicker.MinimumDateTime变为DateTime.Now所述的DateTimePicker对取消的第一下拉事件,因为第二时间(次以后)的下拉显示日历的日历正确的文本框(DateTimePicker.Value),显示的日期相匹配

下面是$ C $下,我已经连接到的DateTimePicker有问题的事件:

 私人无效ValueDatetimePickerOnKeyUp(对象发件人,KeyEventArgs E)
    {
        如果(e.Key code = Keys.Delete和放大器;!&安培;!e.Key code = Keys.Back)
            返回;
        VAR DP =(的DateTimePicker)发送;
        如果(DP == NULL)
            返回;
        dp.Value = DateTimePicker.MinimumDateTime;
    }

    私人无效ValueDatetimePickerDropDown(对象发件人,EventArgs的)
    {
        VAR DP =(的DateTimePicker)发送;
        如果(DP == NULL)
            返回;
        如果(dp.Value == DateTimePicker.MinimumDateTime)
            dp.Value = DateTime.Now;
    }

    私人无效ValueDatetimePickerValueChanged(对象发件人,EventArgs的)
    {
        VAR DP =(的DateTimePicker)发送;
        如果(DP == NULL)
            返回;
        dp.CustomFormat = dp.Value == DateTimePicker.MinimumDateTime? :YYYY-MM-DD HH:MM:SS TT;
    }
 

解决方案

我有一些时间来想出解决办法。这是一个有点哈克,但基本上在的DateTimePicker的下拉事件处理程序设置,将ShowUpDown为true,然后调用特写事件处理程序必须将ShowUpDown设置为false。这将关闭下拉日历并强制用户再次打开它,这将然后有日历,而不是1753年1月1日上显示正确的日期。该onkeyup事件处理程序只允许用户在空白出的DateTimePicker的文本框的值,如果他们打DEL或Backspace键。

 私人无效ValueDatetimePickerOnKeyUp(对象发件人,KeyEventArgs E)
    {
        //如果用户presses退格或删除键,然后清除的日期/时间
        如果(e.Key code = Keys.Delete和放大器;!&安培;!e.Key code = Keys.Back)
            返回;
        VAR DP =(的DateTimePicker)发送;
        如果(DP == NULL)
            返回;
        dp.Value = DateTimePicker.MinimumDateTime;
    }

    私人无效ValueDatetimePickerCloseUp(对象发件人,EventArgs的)
    {
        VAR DP =(的DateTimePicker)发送;
        如果(DP == NULL)
            返回;
        dp.ShowUpDown = FALSE;
    }

    私人无效ValueDatetimePickerDropDown(对象发件人,EventArgs的)
    {
        VAR DP =(的DateTimePicker)发送;
        如果(DP == NULL)
            返回;
        如果(dp.Value == DateTimePicker.MinimumDateTime)
        {
            dp.Value = DateTime.Now;
            dp.ShowUpDown = TRUE;
            调用((MethodInvoker)(()=> ValueDatetimePickerCloseUp(DP,新EventArgs的())));
        }
    }

    私人无效ValueDatetimePickerValueChanged(对象发件人,EventArgs的)
    {
        VAR DP =(的DateTimePicker)发送;
        如果(DP == NULL)
            返回;
        dp.CustomFormat = dp.Value == DateTimePicker.MinimumDateTime? :YYYY-MM-DD HH:MM:SS TT;
    }
 

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";
    }

解决方案

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";
    }

这篇关于我怎么能编程方式关闭一个DateTimePicker的下拉日历或更新下拉日历来反映。价值的财产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆