如何识别用户是否点击了 DateTimePicker 日历中的日期 [英] how to identity if user clicked on a date in DateTimePicker calendar

查看:66
本文介绍了如何识别用户是否点击了 DateTimePicker 日历中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户点击日历而不点击日期时,我不希望日期发生变化.问题是当日期选择器日历打开时总是选择一个日期.我正在日历控件中寻找类似于 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 :( )

就代码而言,这是我所拥有的

As far as code this is what i have

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 上已经至少有 1 个 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.然后当表单加载或您正在重置字段时,将 Checked 设置为 False.这使得 DTP 看起来被禁用,这有点不幸,但是当用户打开 DTP 时,复选框会被自动选中.因此,要查看他们是否选择了日期,您只需评估 Checked 属性.如果他们在选择有效日期后碰巧取消选中该值,则禁用的外观最终表明该值实际上并不重要.

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 日历中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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