如何使 DateTimePicker 显示空字符串?
我希望能够显示一个默认值为零(即没有日期)的 DateTimePicker
。
例如,我有一个任务的开始日期 dtTaskStart
和结束日期 dtTaskEnd
,但结束日期未知,并且最初未填充。
我为这两个控件指定了 yyyy-MM-dd
的自定义格式。
将值设置为null
,或者在运行时设置为空字符串会导致错误,那么我该如何完成此操作呢?
我考虑过使用复选框来控制该字段的启用,但仍然存在显示初始值的问题。
编辑:
可以说是问题的重复 DateTimePicker Null Value (.NET),但我找到的解决方案我的问题不是该问题的解决方案,所以我认为它应该留在这里供其他人找到..
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..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
只需按如下方式设置属性:
当用户按下“清除按钮”或“删除键”时执行
DateTimePicker1_ValueChanged
事件执行Just set the property as follows:
When the user press "clear button" or "delete key" do
On
DateTimePicker1_ValueChanged
event do通过使用
CustomFormat
属性来混淆该值,并使用复选框cbEnableEndDate
作为标志来指示其他代码是否应忽略该值:Obfuscating the value by using the
CustomFormat
property, using checkboxcbEnableEndDate
as the flag to indicate whether other code should ignore the value:这对我来说对 c#
很有用,好人!
this worked for me for c#
nice one guys!
如果有人在表单加载事件期间将 datetimepicker 控件设置为空白,然后根据需要显示当前日期时遇到问题,请参阅以下示例:
请确保
CustomFormat = " "
具有相同数量的两种方法中的空格(至少一个空格)In case anybody has an issue with setting datetimepicker control to blank during the form load event, and then show the current date as needed, here is an example:
MAKE SURE THAT
CustomFormat = " "
has same number of spaces (at least one space) in both methods基本概念和其他人讲的一样。 但当你有多个 dateTimePicker 时,这种方式实现起来更容易。
值更改事件函数
未选中时的
选中后
The basic concept is the same told by others. But its easier to implement this way when you have multiple dateTimePicker.
the value changed event function
When unchecked
When checked
当我想显示空日期值时,我这样做
然后当用户单击控件时,我有这样的:
这允许您显示和使用空日期值,但仍然允许用户能够将日期更改为某些内容当他们愿意的时候。
请记住,sStrDate 已被验证为有效的日期字符串。
When I want to display an empty date value I do this
Then when the user clicks on the control I have this:
This allows you to display and use an empty date value, but still allow the user to be able to change the date to something when they wish.
Keep in mind that sStrDate has already been validated as a valid date string.
听 ,
如果您想显示空的日期时间选择器并在用户未选择日期时获取 null,请在代码中进行以下更改,否则保存日期。
现在
,如果用户与 datetimepicker 交互,其 VALUECHANGED 事件将被调用,并将其 TAG 属性设置为 1。
最后一步
现在,在保存时,检查其 TAG 是否为零,然后保存 NULL 日期,否则如果 TAG 为 1,则选择并保存日期时间选择器值。
它的作用就像一个魅力。
现在,如果您希望通过用户交互将其值更改回空,请添加复选框并使用此复选框显示文本“清除”。
如果用户想要清除日期,只需再次将其CUSTOM FORMAT属性设置为空字符串即可
“ ”,并将其 TAG 设置回 0。
就是这样..
Listen ,
Make Following changes in your code if you want to show empty datetimepicker and get null when no date is selected by user, else save date.
Now real work starts
if user will interact with datetimepicker its VALUECHANGED event will be called and there set its TAG property to 1.
Final Step
Now when saving, check if its TAG is zero, then save NULL date else if TAG is 1 then pick and save Datetime picker value.
It Works like a charm.
Now if you want its value be changed back to empty by user interaction, then add checkbox and show text "Clear" with this checkbox.
if user wants to clear date, simply again set its CUSTOM FORMAT property to empty string
" ", and set its TAG back to 0.
Thats it..
最好使用文本框来调用/显示日期,并在保存时使用 DateTimePicker。
根据要求将可见属性设为 true 或 false。
例如:在表单加载期间,在文本框中加载日期并使 DTPIcker 不可见,同时添加反之亦然
Better to use text box for calling/displaying date and while saving use DateTimePicker.
Make visible property true or false as per requirement.
For eg : During form load make Load date in Textbox and make DTPIcker invisible and while adding vice versa