简体   繁体   中英

Setting the Date Time Picker to blank or 0 value

I have this form right here, where the user will add the start date of the diagnosis. pretty simple right? but in the end diagnosis how would I set it to a blank? or 0 value? here's my form. 在此处输入图片说明

The best you can do is to set ShowCheckBox to true. If the CheckBox is not checked, consider that no end date is set.

i think, it is not possible to set it as blank or 0 value.. the least you can do is to set it in its MinDate if it's null..

this one works for me..

 dateTimePicker1.Format = DateTimePickerFormat.Custom;

 dateTimePicker1.CustomFormat = " ";

you don't want it to be set to 0. It makes no sense (1/1/01 ? most of DB doesn't event store this date)

You should put it either the same as start or in a reasonable margin which describe the common scenario (like + 2 weeks)

try to use

dateTimePicker1.Value.Date

It would return you only date

All solved now just had to adjust. As what you guys were saying i wasn't setting the value or the properties so it would all ways be null anyway

private string _BirthDate;

        public Form1()
        {
            InitializeComponent();
            dateTimePicker1.MaxDate = DateTime.Now; // Ensures no future dates are set.
            dateTimePicker1.Format = DateTimePickerFormat.Custom;
            //dateTimePicker1.Checked = true;
            dateTimePicker1.CustomFormat = dateTimePicker1.CustomFormat;
            dateTimePicker1.Value = DateTime.Now;
        }
        public string BirthDate
        {
            set
            {
                if (dateTimePicker1.Value.Date == DateTime.Today)
                {
                    dateTimePicker1.CustomFormat = " ";

                }
                else
                {
                    _BirthDate = value;
                }
            }
            get { return _BirthDate; }
        }


        private void button1_Click(object sender, EventArgs e)
        {

           BirthDate = dateTimePicker1.Value.ToString();
        }

Try setting the Value property . However I think there is a limit on how far back you can set the value.

当您希望表示无日期时,可以放置一个外部复选框并禁用datetimepicker。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM