如何使 DateTimePicker 显示空字符串?

发布于 2024-07-21 04:40:16 字数 522 浏览 9 评论 0原文

我希望能够显示一个默认值为零(即没有日期)的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

凤舞天涯 2024-07-28 04:40:16

只需按如下方式设置属性:

当用户按下“清除按钮”或“删除键”时执行

dtpData.CustomFormat = " "  'An empty SPACE
dtpData.Format = DateTimePickerFormat.Custom

DateTimePicker1_ValueChanged事件执行

dtpData.CustomFormat = "dd/MM/yyyy hh:mm:ss"

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_ValueChanged event do

dtpData.CustomFormat = "dd/MM/yyyy hh:mm:ss"
此刻的回忆 2024-07-28 04:40:16

通过使用 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

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
孤凫 2024-07-28 04:40:16

这对我来说对 c#

if (enableEndDateCheckBox.Checked == true)
{
    endDateDateTimePicker.Enabled = true;
    endDateDateTimePicker.Format = DateTimePickerFormat.Short;                
}
else
{
    endDateDateTimePicker.Enabled = false;
    endDateDateTimePicker.Format = DateTimePickerFormat.Custom;
    endDateDateTimePicker.CustomFormat = " ";
}

很有用,好人!

this worked for me for 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!

故人如初 2024-07-28 04:40:16

如果有人在表单加载事件期间将 datetimepicker 控件设置为空白,然后根据需要显示当前日期时遇到问题,请参阅以下示例:

请确保 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

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

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
甩你一脸翔 2024-07-28 04:40:16

基本概念和其他人讲的一样。 但当你有多个 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;

值更改事件函数

        void Dtp_ValueChanged(object sender, EventArgs e)
        {
            DateTimePicker dtp = (DateTimePicker)sender;
            if (dtp.ShowCheckBox)
            {
                if (dtp.Checked)
                {
                    dtp.Format = DateTimePickerFormat.Short;
                }
                else
                {
                    dtp.CustomFormat = " ";
                    dtp.Format = DateTimePickerFormat.Custom;
                }
            }
            else
            {
                dtp.Format = DateTimePickerFormat.Short;
            }
        }

未选中时的
输入图片此处描述

选中后
输入图片此处描述

The basic concept is the same told by others. But its easier to implement this way when you have multiple 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)
        {
            DateTimePicker dtp = (DateTimePicker)sender;
            if (dtp.ShowCheckBox)
            {
                if (dtp.Checked)
                {
                    dtp.Format = DateTimePickerFormat.Short;
                }
                else
                {
                    dtp.CustomFormat = " ";
                    dtp.Format = DateTimePickerFormat.Custom;
                }
            }
            else
            {
                dtp.Format = DateTimePickerFormat.Short;
            }
        }

When unchecked
enter image description here

When checked
enter image description here

℡Ms空城旧梦 2024-07-28 04:40:16

当我想显示空日期值时,我这样做

            if (sStrDate != "")
            {
                dateCreated.Value = DateTime.Parse(sStrDate);
            }
            else
            {
                dateCreated.CustomFormat = " ";
                dateCreated.Format = DateTimePickerFormat.Custom;
            }

然后当用户单击控件时,我有这样的:

            private void dateControl_MouseDown(object sender, MouseEventArgs e)
            {
                ((DateTimePicker)sender).Format = DateTimePickerFormat.Long;    
            }

这允许您显示和使用空日期值,但仍然允许用户能够将日期更改为某些内容当他们愿意的时候。

请记住,sStrDate 已被验证为有效的日期字符串。

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.

讽刺将军 2024-07-28 04:40:16

听 ,
如果您想显示空的日期时间选择器并在用户未选择日期时获取 null,请在代码中进行以下更改,否则保存日期。

  1. 将 datetimepicker FORMAT 属性设置为自定义。
  2. CUSTOM FORMAT属性设置为空字符串“”。
  3. 默认情况下将其 TAG 设置为 0。
  4. 为日期时间选择器VALUECHANGED注册事件

现在

,如果用户与 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.

  1. Set datetimepicker FORMAT property to custom.
  2. set CUSTOM FORMAT property to empty string " ".
  3. set its TAG to 0 by default.
  4. Register Event for datetimepicker VALUECHANGED.

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..

动听の歌 2024-07-28 04:40:16

最好使用文本框来调用/显示日期,并在保存时使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文