.net 如何让 DateTimePicker 显示空字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/846395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make a DateTimePicker display an empty string?
提问by brasskazoo
I would like to be able to display a DateTimePickerthat has a default value of nothing, i.e. no date.
我希望能够显示DateTimePicker一个默认值为空的,即没有日期。
For example, I have a start date dtTaskStartand an end date dtTaskEndfor a task, but the end date is not known, and not populated initially.
例如,我有一个任务的开始日期dtTaskStart和结束日期dtTaskEnd,但结束日期未知,并且最初没有填充。
I have specified a custom format of yyyy-MM-ddfor both controls.
我已经yyyy-MM-dd为两个控件指定了自定义格式。
Setting the value to null, or an empty string at runtime causes an error, so how can I accomplish this?
null在运行时将值设置为或空字符串会导致错误,那么我该如何实现呢?
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..
编辑:
可以说是问题DateTimePicker Null Value (.NET)的重复,但我为我的问题找到的解决方案不是该问题的解决方案,所以我认为它应该保留在这里供其他人找到..
采纳答案by brasskazoo
Obfuscating the value by using the CustomFormatproperty, using checkbox cbEnableEndDateas the flag to indicate whether other code should ignore the value:
使用CustomFormat属性混淆值,使用复选框cbEnableEndDate作为标志来指示其他代码是否应该忽略该值:
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
回答by Sérgio Domingos
Just set the property as follows:
只需按如下方式设置属性:
When the user press "clear button" or "delete key" do
当用户按下“清除键”或“删除键”时
dtpData.CustomFormat = " " 'An empty SPACE
dtpData.Format = DateTimePickerFormat.Custom
On DateTimePicker1_ValueChangedevent do
在DateTimePicker1_ValueChanged事件做
dtpData.CustomFormat = "dd/MM/yyyy hh:mm:ss"
回答by David Swindells
this worked for me for c#
这对我有用 C#
if (enableEndDateCheckBox.Checked == true)
{
endDateDateTimePicker.Enabled = true;
endDateDateTimePicker.Format = DateTimePickerFormat.Short;
}
else
{
endDateDateTimePicker.Enabled = false;
endDateDateTimePicker.Format = DateTimePickerFormat.Custom;
endDateDateTimePicker.CustomFormat = " ";
}
nice one guys!
好一个人!
回答by Catherine
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:
如果有人在表单加载事件期间将 datetimepicker 控件设置为空白,然后根据需要显示当前日期,则这里是一个示例:
MAKE SURE THAT CustomFormat = " "has same number of spaces (at least one space) in both methods
确保CustomFormat = " "在两种方法中具有相同数量的空格(至少一个空格)
Private Sub setDateTimePickerBlank(ByVal dateTimePicker As DateTimePicker)
dateTimePicker.Visible = True
dateTimePicker.Format = DateTimePickerFormat.Custom
dateTimePicker.CustomFormat = " "
End Sub
Private Sub dateTimePicker_MouseHover(ByVal sender As Object, ByVal e As
System.EventArgs) Handles dateTimePicker.MouseHover
Dim dateTimePicker As DateTimePicker = CType(sender, DateTimePicker)
If dateTimePicker.Text = " " Then
dateTimePicker.Text = Format(DateTime.Now, "MM/dd/yyyy")
End If
End Sub
回答by CA Martin
When I want to display an empty date value I do this
当我想显示一个空的日期值时,我会这样做
if (sStrDate != "")
{
dateCreated.Value = DateTime.Parse(sStrDate);
}
else
{
dateCreated.CustomFormat = " ";
dateCreated.Format = DateTimePickerFormat.Custom;
}
Then when the user clicks on the control I have this:
然后当用户点击控件时,我有这个:
private void dateControl_MouseDown(object sender, MouseEventArgs e)
{
((DateTimePicker)sender).Format = DateTimePickerFormat.Long;
}
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.
请记住, sStrDate 已被验证为有效的日期字符串。
回答by Rijul Sudhir
The basic concept is the same told by others. But its easier to implement this way when you have multiple dateTimePicker.
基本概念和其他人说的一样。但是当您有多个 dateTimePicker 时,这种方式更容易实现。
dateTimePicker1.Value = DateTime.Now;
dateTimePicker1.ValueChanged += new System.EventHandler(this.Dtp_ValueChanged);
dateTimePicker1.ShowCheckBox=true;
dateTimePicker1.Checked=false;
dateTimePicker2.Value = DateTime.Now;
dateTimePicker2.ValueChanged += new System.EventHandler(this.Dtp_ValueChanged);
dateTimePicker2.ShowCheckBox=true;
dateTimePicker2.Checked=false;
the value changed event function
值改变事件函数
void Dtp_ValueChanged(object sender, EventArgs e)
{
if(((DateTimePicker)sender).ShowCheckBox==true)
{
if(((DateTimePicker)sender).Checked==false)
{
((DateTimePicker)sender).CustomFormat = " ";
((DateTimePicker)sender).Format = DateTimePickerFormat.Custom;
}
else
{
((DateTimePicker)sender).Format = DateTimePickerFormat.Short;
}
}
else
{
((DateTimePicker)sender).Format = DateTimePickerFormat.Short;
}
}
回答by umer
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.
听,如果您想显示空的日期时间选择器并在用户未选择日期时获取空值,请在代码中进行以下更改,否则保存日期。
- Set datetimepicker FORMATproperty to custom.
- set CUSTOM FORMATproperty to empty string " ".
- set its TAGto 0 by default.
- Register Eventfor datetimepicker VALUECHANGED.
- 将 datetimepicker FORMAT属性设置为自定义。
- 将CUSTOM FORMAT属性设置为空字符串“”。
- 默认情况下将其TAG设置为 0。
- 为日期时间选择器 VALUECHANGED注册事件。
Now real work starts
现在真正的工作开始了
if user will interact with datetimepicker its VALUECHANGEDevent will be called and there set its TAG property to 1.
如果用户将与 datetimepicker 交互,则将调用其VALUECHANGED事件并将其 TAG 属性设置为 1。
Final Step
最后一步
Now when saving, check if its TAGis zero, then save NULL date else if TAGis 1 then pick and save Datetime picker value.
现在保存时,检查其TAG是否为零,然后保存 NULL 日期,如果TAG为 1,则选择并保存日期时间选择器值。
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 FORMATproperty to empty string " ", and set its TAGback to 0. Thats it..
现在,如果您希望通过用户交互将其值更改回空,请添加复选框并使用此复选框显示文本“清除”。如果用户想要清除日期,只需再次将其CUSTOM FORMAT属性设置为空字符串“”,并将其TAG设置回 0。就是这样..
回答by V Venkata Ramana
Better to use text box for calling/displaying date and while saving use DateTimePicker. Make visible property true or false as per requirement.
最好使用文本框来调用/显示日期并在保存时使用 DateTimePicker。根据要求使可见属性为真或假。
For eg : During form load make Load date in Textbox and make DTPIcker invisible and while adding vice versa
例如:在表单加载期间,在文本框中设置加载日期并使 DTPIcker 不可见,反之亦然


