简体   繁体   English

如何以编程方式关闭 datetimepicker 的下拉日历或更新下拉日历以反映 .Value 属性?

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

HELP, Please?!请帮助?! The issue is because I have an old usercontrol that makes use of a datetimepicker control.问题是因为我有一个使用 datetimepicker 控件的旧用户控件。 If there is no date to be displayed in the textbox of the datetimepicker then the .Value property is set to DateTimePicker.MinimumDateTime.如果 datetimepicker 的文本框中没有要显示的日期,则 .Value 属性设置为 DateTimePicker.MinimumDateTime。 OnValueChanged will update the CustomFormat to " " if the .Value is DateTimePicker.MinimumDateTime.如果 .Value 是 DateTimePicker.MinimumDateTime,OnValueChanged 会将 CustomFormat 更新为“”。 Otherwise, the CustomFormat is "yyy-MM-dd hh:mm:ss tt".否则,CustomFormat 为“yyy-MM-dd hh:mm:ss tt”。

Problem ==> In the DropDown event I check for the minimum datetime.问题 ==> 在 DropDown 事件中,我检查了最小日期时间。 If the .Value is equal to that then I update the .Value to be DateTime.Now.如果 .Value 等于那个,那么我将 .Value 更新为 DateTime.Now。 When the dropdown calendar is shown the the calendar is set for 1753-01-01, while the textbox (.Value) shows DateTime.Now.当显示下拉日历时,日历设置为 1753-01-01,而文本框 (.Value) 显示 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?如何让日历显示对应于 DropDown 事件中更新的 .Value 属性的日期? 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).即使当值从 DateTimePicker.MinimumDateTime 更改为 DateTime.Now 时,有一种方法可以“取消” DateTimePicker 的第一个 DropDown 事件,我认为这可以工作,因为第二次(以及随后的时间)下拉显示日历 日历与文本框 (DateTimePicker.Value) 中显示的日期正确匹配。

Here is the code for the events that I have wired up to the DateTimePicker in question :这是我连接到相关 DateTimePicker 的事件的代码:

    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.这有点hacky,但基本上在datetimepicker 的DropDown 事件处理程序中将ShowUpDown 设置为true,然后调用Closeup 事件处理程序将ShowUpDown 设置回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.这将关闭下拉日历并强制用户再次打开它,然后日历上将显示正确的日期,而不是 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. OnKeyUp 事件处理程序只允许用户在按下 DEL 或 Backspace 键时将 datetimepicker 的文本框值清空。

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

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.这将允许您要在 KeyDown 事件上传递的所有键。 ;D hf!!! ;D 高频!!! AND WORKS WITH ANOTHER CONTROLS TOO, like TextBox, DataGridView, etc.并且还可以与其他控件一起使用,例如 TextBox、DataGridView 等。

If in need only to forbid drop down menu to be open, you might use simpler version of the solution.如果只需要禁止打开下拉菜单,您可以使用更简单的解决方案。

Just switch ShowUpDown property in DropDown event.只需在DropDown事件中切换ShowUpDown属性。 Change this to your control name.将此更改为您的控件名称。

private void MyDropDown(object sender, EventArgs e)
{
    bool value = this.ShowUpDown;
    this.SuspendLayout();
    this.ShowUpDown = !value;
    this.ShowUpDown = value;
    this.ResumeLayout();
}

Add event handler.添加事件处理程序。

this.DropDown += MyDropDown;

It might be handy to set DoubleBuffered property to true to avoid flickering.将 DoubleBuffered 属性设置为 true 以避免闪烁可能会很方便。

Type type = this.GetType();
System.Reflection.PropertyInfo property;
property = type.GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
property.SetValue(control, true, null);

Alternatively you might check if ShowUpDown property is already false, so it will only work for calendar drop down.或者,您可以检查ShowUpDown属性是否已经为 false,因此它仅适用于日历下拉列表。

private void MyDropDown(object sender, EventArgs e)
{
    if (this.ShowUpDown == false)
    {
        this.SuspendLayout();
        this.ShowUpDown = true;
        this.ShowUpDown = false;
        this.ResumeLayout();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在ValueChanged事件中以编程方式关闭/关闭datetimepicker的下拉日历? - How can I programmatically close/dismiss the dropdown calendar of a datetimepicker on ValueChanged event? 当用户单击DropDown按钮时,如何不在DateTimePicker中显示日历 - How not to show calendar in the DateTimePicker when user clicks DropDown button 显示带有派生的DateTimePicker类的自定义日历下拉列表 - Show a custom calendar dropdown with a derived DateTimePicker class 如何将 datetimepicker 下拉菜单设置为仅显示月份 - How can I set the datetimepicker dropdown to show Months only 如何将 DateTimePicker 下拉菜单设置为 select 年或月? - How can I set the DateTimePicker dropdown to select Years or Months only? 如何在 C# 和 WPF 中制作日历下拉菜单? - How to make calendar dropdown in C# and WPF? 如何在DateTimePicker控件中自定义日历? - How to customize Calendar in DateTimePicker Control? 如何显示英文的DateTimePicker日历? - How to display Calendar of DateTimePicker to English? 如何以编程方式关闭DateTimePicker的日历 - How to programatically dismiss the calendar of a DateTimePicker DateTimePicker:如果用户在弹出式日历中单击/选择日期,该如何处理? - DateTimePicker: How will I handle if the user clicked/selected date in the popup calendar or not?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM