如何以编程方式关闭DateTimePicker的日历

How to programatically dismiss the calendar of a DateTimePicker
2020-11-25
  •  译文(汉语)
  •  原文(英语)

我正在使用大量DateTimePicker控件的Windows窗体上工作.根据规格,控制器需要在用户手动选择日期之前显示"无日期".

根据DateTimePicker功能,我们无法将"无"指定为值.因此,我们要做的是设置默认的最小日期,然后将控件的自定义格式更改为"",以产生空字段的错觉.

我们发现的一个怪癖是,当用户单击DateTimePicker并第一次选择新日期时,其初始单击不会关闭日历.因此,日历仍然存在,并且用户必须在控件外部手动单击以关闭日历.

为使用户体验稍差,在此状态下,日历不受任何其他日期选择的影响.因此,日期选择与最初选择的日期保持一致,用户需要单击.

控件选择了日期后,选择新日期将按预期关闭日历.

我最好的猜测是,这将有事可做与自定义格式,本来有,当选择一个新的格式正确的日期这会导致与控制打嗝.

我正在尝试找到一种方法,以"强制"日历在打开值更改时被解雇,但我没有找到这样做的方法.我什至试图将重点放在价值变化上,但也没有运气.

是否有其他人经历过这种行为和/或知道解决方法?谢谢!

一些代码-----------

//if dateTime property has not been saved, then call ClearDateTime on the controller, else display the saved value.

public static void ClearDateTime(DateTimePicker dtp)
    {
        dtp.Value = DateTime.Today.AddDays(-1);
        //set the format
        dtp.Format = System.Windows.Forms.DateTimePickerFormat.Custom;

        // set custom format to empty
        dtp.CustomFormat = " " ;
    }

// When value is changed/date is selected
private void DateTimePicker_ValueChanged(object sender, EventArgs e)
    {
        MainFormUtilities.ShowDateTime(this.DateTimePicker);
        this.LockFormDown();
    }

public static void ShowDateTime(DateTimePicker dtp)
    {
        dtp.Format = System.Windows.Forms.DateTimePickerFormat.Long;
    }
速聊1:
我已经尝试过复制但不能复制.我设法设置了一个最小日期并按照您的建议隐藏了文本,但是随后我放入dateTimePicker1.Format = DateTimePickerFormat.LongdateTimePicker1_ValueChanged事件,它的行为就像您期望的选择日期一样.您可以显示您的初始代码和选定代码吗?
速聊2:
在这种情况下,我会在日期时间选择器(DTP)附近添加复选框.选中的框将启用DTP,而未选中的未设置DTP("无"值)
速聊3:
,我已经添加了一些代码.希望它能给出正在发生的事情的想法.
速聊4:
.那实际上是我所得到的解决方案.似乎可以解决一些用户体验问题,而我们无需使用定制的控制器就可以解决DateTimePicker问题.谢谢!
速聊5:
别客气.当然,带有combobox + DTP的自定义控件比2个控件要方便得多.;)
解决过程1

本质上,您需要手动引发CloseUp事件,请参考此处的解决方案,如何以编程方式关闭datetimepicker的下拉日历或更新下拉日历以反映.Value属性?.

解决过程2

我遇到过类似的情况,客户坚持要求该字段不显示日期(显示为空白).我放弃尝试破解标准的.NET DateTimePicker并使用了DevExpress DateEdit控件,该控件确实允许空白值.

解决过程3

我追求的对我的项目最实用的解决方案是Reniuz的建议,即使用复选框来控制DateTimePicker的值和行为.如果用户要添加日期,则选择复选框以启用选择器,并默认为今天的日期.如果他想清除日期(即根本不提交日期),则取消选中该复选框,该复选框会以空格式清除选择器,并且我们不将任何内容保存到数据库中.

如果我有观点,我会否决所有其他建议.他们是有帮助的,但是这种解决方案似乎是我们的实际方法.再次感谢所有人!

I am working on a Windows Form that uses a good amount of DateTimePicker controls. As per spec, the controllers need to show 'no date' initially till user manually selects a date.

As per the DateTimePicker functionality, we cant assign 'nothing' as a value. So what we do is set a default minimum date, and then change the custom format of the control to "", to give the illusion of an empty field.

A quirk we have found is that when the user clicks on the DateTimePicker and selects a new date for the first time, his initial click does not dismiss the calendar. So instead the calendar persists, and the user has to manually click outside the control to dismiss the calendar.

To make the user experience slightly worse, while in this state the calendar is immune to any further date selection. So the date selection is stuck with the initially selected date, and the user needs to click out.

Once a date has been selected by the control, selecting a new date dismisses the calendar as expected.

My best guess would be that it would have something to do with the custom format that is there originally, which causes a hiccup with the control when a new properly formatted date is selected.

I am trying to find a way to 'force' the calendar to dismiss if open on value change, but I am not finding the way to do this. I have even tried to set focus elsewhere on value change, but no luck either.

Has anybody else experienced this behavior and/or know of a way around it? Thanks!

Some of the code-----------

//if dateTime property has not been saved, then call ClearDateTime on the controller, else display the saved value.

public static void ClearDateTime(DateTimePicker dtp)
    {
        dtp.Value = DateTime.Today.AddDays(-1);
        //set the format
        dtp.Format = System.Windows.Forms.DateTimePickerFormat.Custom;

        // set custom format to empty
        dtp.CustomFormat = " " ;
    }

// When value is changed/date is selected
private void DateTimePicker_ValueChanged(object sender, EventArgs e)
    {
        MainFormUtilities.ShowDateTime(this.DateTimePicker);
        this.LockFormDown();
    }

public static void ShowDateTime(DateTimePicker dtp)
    {
        dtp.Format = System.Windows.Forms.DateTimePickerFormat.Long;
    }
Talk1:
I've tried to replicate this but can't. I managed to set a min date and hide the text like you suggested, but then I put dateTimePicker1.Format = DateTimePickerFormat.Long in the dateTimePicker1_ValueChanged event and it behaved like you'd expect on selecting a date. Can you show your initial and on-select code?
Talk2:
In this case I would add check box near date time picker(DTP). Checked box would enable DTP and unchecked not set DTP ('nothing' value)
Talk3:
, Ive added some of the code. hopefully it gives the idea of what is happening.
Talk4:
. that was actually the solution I have resulted to. Seems to solve a couple of user experience problems we have been experience with our slightly hacked DateTimePicker problems, without spending money on a custom built controller. Thanks you!
Talk5:
You are welcome. Of course custom control with combobox + DTP will be very handy than 2 controls. ;)
Solutions1

Essentially you need to manually raise the CloseUp event, please refer to the solutions here, How can I programmatically close the dropdown calendar of a datetimepicker or update the dropdown calendar to reflect the .Value property?.

Solutions2

I had a similar situation where a client insisted on having the field show no date (appear blank). I gave up on trying to hack the standard .NET DateTimePicker and used the DevExpress DateEdit control which does allow a blank value.

Solutions3

The solution I have pursued that was most practical for my project was the suggestion by Reniuz, to use a checkbox to control the values and behaviour of the DateTimePicker. If a user wants to add a date, he selects the checkbox which enables the Picker and defaults to todays date. If he wants to clear the date (ie not submit a date after all) he unchecks the checkbox which clears the picker with a empty format and we save nothing to the database.

If I had the points I would upvote all the other suggestions. They were helpful, but this solution seemed to be the practical path for us. Thanks to all again!

转载于:https://stackoverflow.com/questions/28091875/how-to-programatically-dismiss-the-calendar-of-a-datetimepicker

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入