如何让 DateTimePicker 显示空字符串? [英] How can I make a DateTimePicker display an empty string?

查看:23
本文介绍了如何让 DateTimePicker 显示空字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够显示默认值为空的 DateTimePicker,即没有日期.

例如,我有一个任务的开始日期 dtTaskStart 和结束日期 dtTaskEnd,但结束日期未知,并且最初未填充.>

我已经为两个控件指定了 yyyy-MM-dd 的自定义格式.

在运行时将值设置为 null 或空字符串会导致错误,那么我该如何实现?

我已经考虑过使用复选框来控制该字段的启用,但仍然存在显示初始值的问题..


可以说是问题 DateTimePicker Null Value (.NET) 的重复,但我找到的解决方案我的问题不是那个问题的解决方案,所以我认为它应该留在这里供其他人找到..

解决方案

通过使用 CustomFormat 属性混淆值,使用复选框 cbEnableEndDate 作为标志来指示其他代码应忽略该值:

如果 dateTaskEnd >Date.FromOADate(0) 然后dtTaskEnd.Format = DateTimePickerFormat.CustomdtTaskEnd.CustomFormat = "yyyy-MM-dd"dtTaskEnd.Value = dateTaskEnddtTaskEnd.Enabled = TruecbEnableEndDate.Checked = True别的dtTaskEnd.Format = DateTimePickerFormat.CustomdtTaskEnd.CustomFormat = " "dtTaskEnd.Value = Date.FromOADate(0)dtTaskEnd.Enabled = FalsecbEnableEndDate.Checked = False万一

I would like to be able to display a DateTimePicker that has a default value of nothing, i.e. no date.

For example, I have a start date dtTaskStart and an end date dtTaskEnd for a task, but the end date is not known, and not populated initially.

I have specified a custom format of yyyy-MM-dd for both controls.

Setting the value to null, or an empty string at runtime causes an error, so how can I accomplish this?

I have considered using a checkbox to control the enabling of this field, but there is still the issue of displaying an initial value..

Edit:
Arguably a duplicate of the question DateTimePicker Null Value (.NET), but the solution I found for my problem is not a solution for that question, so I think it should remain here for others to find..

解决方案

Obfuscating the value by using the CustomFormat property, using checkbox cbEnableEndDate as the flag to indicate whether other code should ignore the value:

If dateTaskEnd > Date.FromOADate(0) Then
    dtTaskEnd.Format = DateTimePickerFormat.Custom
    dtTaskEnd.CustomFormat = "yyyy-MM-dd"
    dtTaskEnd.Value = dateTaskEnd 
    dtTaskEnd.Enabled = True
    cbEnableEndDate.Checked = True
Else
    dtTaskEnd.Format = DateTimePickerFormat.Custom
    dtTaskEnd.CustomFormat = " "
    dtTaskEnd.Value = Date.FromOADate(0)
    dtTaskEnd.Enabled = False
    cbEnableEndDate.Checked = False
End If

这篇关于如何让 DateTimePicker 显示空字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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