问题描述
当用户点击日历而不点击日期时,我不希望更改日期。问题是在Datepickercalendar打开时始终选择One Date。我正在寻找类似于日历控件中的DateSelected事件的类型。
I don't want the date to change when user clicks out of the calender without clicking on a date. The problem is One Date is always selected when the datepickercalendar opens up. I am looking for something similar to DateSelected event in the Calender control
我有一个派生自DateTimePicker的类,它使用带有空格键的自定义格式来具有空字段DateTimePicker。
I have a class that derives from DateTimePicker which uses custom format with a spacechar string for to have empty field for DateTimePicker.
我在stackoverflow中找到了一个同样问题的文章,但解决方案不工作。我没有足够的分数来发表评论(无法找到链接现在:()
I found an article in stackoverflow with same issue but the solution doesnot work. I don't have enough points to place comments either (unable to find the link now :( )
至于代码这是我有的
Public Class CustomDP
Inherits System.Windows.Forms.DateTimePicker
Public Sub New()
CustomFormat = " "
Format = System.Windows.Forms.DateTimePickerFormat.Custom
End Sub
Protected Overrides Sub OnValueChanged(eventargs As System.EventArgs)
MyBase.OnValueChanged(eventargs)
End Sub
Protected Overrides Sub OnCloseUp(eventargs As System.EventArgs)
Format = DateTimePickerFormat.Short
MyBase.OnCloseUp(eventargs)
End Sub
Protected Overrides Sub OnDropDown(eventargs As System.EventArgs)
Format = DateTimePickerFormat.Custom
MyBase.OnDropDown(eventargs)
End Sub
End Class
推荐答案
CodeProject上已经有至少一个$ code> DateTimePicker 控件,该对象允许一个Nullable Date。但是有一个比这更简单的方法。
There is already at least 1 DateTimePicker
control on CodeProject which allows for a Nullable Date. But there is a simpler method than that.
在设计中,将 ShowCheckBox
设置为True。然后当表单加载或重新设置字段时,将选中
设置为False。这使得DTP看起来被禁用,这是不幸的,但是当用户打开DTP时,将自动检查复选框。所以,所有你需要做的,看他们是否选择一个日期,是评估检查
属性。禁用的外观最终表明,该值无法真正计数,如果它们在选择有效日期后有可能取消选中。
In design, set the ShowCheckBox
to True. Then when the form loads or you are resetting fields, set Checked
to False. This makes the DTP look disabled which is a little unfortunate, but when the user opens the DTP, the Checkbox is automatically checked. So, all you need to do to see if they picked a date, is to evaluate the Checked
property. The disabled appearance ends up indicating that the value doesnt really count, if they should happen to uncheck it after selecting a valid date.
如果您使用较小的版本( ShowUpDown
),控件 被禁用,直到他们首先点击该复选框,然后选择一个字段来旋转。您仍然可以为每个字段的每个更改获取值更改的事件,但这是如何应用的。控件始终具有有效的日期值,因此您不必检查 Nothing
等。
If you use the smaller version (ShowUpDown
), the control is disabled until they first click the checkbox, then select a field to spin thru. You will still get a value changed event for each change of each field, but thats how it is supposed to work. The control always has a valid date value so you dont have to check against Nothing
etc.
这篇关于如果用户点击DateTimePicker日历中的日期,如何识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!