vb.net 加载时如何将空值设置为Datetimepicker?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23083481/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:23:17  来源:igfitidea点击:

How to set null value to Datetimepicker when load?

vb.net

提问by user3198588

I have a Datetimepicker1, i want to set it to empty when load, and show value after chose. I have found a code

我有一个Datetimepicker1,我想在加载时将其设置为空,并在选择后显示值。我找到了一个代码

        Datetimepicker1.Format = DateTimePickerFormat.Custom
        Datetimepicker1.CustomFormat = " " 

but it does not show value after chose.

但它在选择后不显示价值。

回答by Nayeem Mansoori

DateTimePickers can't be set to null because DateTime can't be null, but you can set them to DateTime.MinValue which is the default value for an uninitialized DateTime. And then you just check if the dtp.Value = DateTime.MinValue and if so, treat it as null.

DateTimePickers 不能设置为 null,因为 DateTime 不能为 null,但您可以将它们设置为 DateTime.MinValue,这是未初始化的 DateTime 的默认值。然后您只需检查 dtp.Value = DateTime.MinValue 是否,如果是,则将其视为空值。

However, if you want to really distinguish when no value has been selected, the easiest way is to set DateTimePicker.ShowCheckBox to true, and then you check dtp.Checked and if it's true, you read the value, otherwise you treat it as a null.

但是,如果要真正区分什么时候没有选择值,最简单的方法是将 DateTimePicker.ShowCheckBox 设置为 true,然后检查 dtp.Checked,如果为 true,则读取该值,否则将其视为一个空值。

Have you try this : for testing purpose

你有没有试过这个:为了测试目的

DateTime? myselectedDate = null;

private void DateTimePicker1_ValueChanged(Object sender, EventArgs e) {

   myselectedDate = DateTimePicker1.Value;

}

And

this.dateTimePicker1.Format = DateTimePickerFormat.Custom;
this.dateTimePicker1.CustomFormat = " "; //a string with one whitespace

回答by AttaKhan

Try this

尝试这个

DateTime? dateSample= null;

约会时间?日期样本=空;

回答by SysDragon

DatetimePicker can't have no value. Null or default value is DateTime.MinValue. Your code changes the format in order to show nothing.

DatetimePicker 不能没有价值。空值或默认值为DateTime.MinValue。您的代码更改格式以不显示任何内容。

To show the date again, simply change DateTimePicker.Formatagain when needed:

要再次显示日期,只需DateTimePicker.Format在需要时再次更改:

DateTimePicker1.Format = DateTimePickerFormat.Long;