none
How to use DTS_SHOWNONE

    Question

  • I have a problem of making a DateTimePicker control to support null/empty value.

     

    I will have to make it 2 ways, which are :

     

    1. User input on form and let the DateTimePicker null/empty. Then, I will save it to String type as "-".

     

    2. If later the user edit the form, I must load all the user input value including the String value of "-", so there will be a checking condition, if String == "-" then the DateTime will be null/empty..

     

    Someone told me to use DTS_SHOWNONE which is the DateTimePicker style.

     

    I have read it here:

     

    but I don't understand how to implement it to my codes..

     

    does anybody know how to implement it? If it possible, please give me along with the code..

     

    Thank you very much..

     

    Friday, January 18, 2008 1:19 AM

Answers

  • The DTS_SHOWNONE style is the same as the ShowCheckBox property, you can do something like this to get a nullable DateTimePicker,

    Code Snippet


        public
    class NullableDateTimePicker : DateTimePicker

        {

            public NullableDateTimePicker()

            {

                this.ShowCheckBox = true;

                this.Checked = false;

            }

     

            public new object Value

            {

                get {

                    if (this.Checked)

                    {

                        return base.Value; ;

                    }

                    else

                    {

                        return null;

                    }

                }

                set { base.Value = (DateTime)value; }

            }

        }

     

     



    However, if you want the DateTimePicker to show empty string if no date selected, you can override the Format and CustomFormat properties, for more details, see this article: Yet Another Nullable DateTimePicker Control

    Wednesday, January 23, 2008 3:05 AM