WinForm DateTimePicker Blues. It's me?

I want to be able to accept a NULL date using the DateTimePicker control.

The Checked property is used to indicate whether the control will “contain a date” or now. However, when "unchecked", the date still appears, although it is disabled. This is distracting for me. If the purpose of the unchecked checkbox indicates that there is no date value, why is there any date value that is disabled or otherwise it appears in the text box? It seems to me that if the control is not checked, the text field should be EMPTY and that the view of the slow date value when the user really wants "no value" is distracting.

If the user has enabled this checkbox, then I would like to put the default value in the text box.

I am considering creating a usercontrol that switches between the dateTimePicker control and the text box, but I can't wait to overcome this problem.

I tried looking for Telerik DateTimePicker, but trying to get decent null information processing functionality from this control seems worse. I would like to see a working example of what one of you considers a user-friendly code example, either using std MS or Telerik DateTimePicker, which accepts zero input.

I looked at several opensource controls, but each time they fix one problem, they introduce others.

EDIT:

See my answer below. It seems to be working fine, now I just want to make it part of every DateTimePicker behavior.

+4
source share
4 answers

A little hard to understand. It looked good:

Imports System.ComponentModel Public Class MyDateTimePicker Inherits DateTimePicker Implements ISupportInitialize Public Sub New() Me.ShowCheckBox = True Me.NullDate = True End Sub Private CustomFormatBacking As String = "" Private FormatBacking As DateTimePickerFormat = DateTimePickerFormat.Long <DefaultValue(True)> _ <Bindable(True)> _ Public Property NullDate() As Boolean Get Return Not Me.Checked End Get Set(ByVal value As Boolean) Me.Checked = Not value End Set End Property <DefaultValue("")> _ <Localizable(True)> _ <RefreshProperties(RefreshProperties.Repaint)> _ Public Shadows Property CustomFormat() As String Get Return CustomFormatBacking End Get Set(ByVal value As String) CustomFormatBacking = value If DesignMode Or Not NullDate Then MyBase.CustomFormat = value End Set End Property <RefreshProperties(RefreshProperties.Repaint)> _ Public Shadows Property Format() As DateTimePickerFormat Get Return FormatBacking End Get Set(ByVal value As DateTimePickerFormat) FormatBacking = value If DesignMode Or Not NullDate Then MyBase.Format = value End Set End Property <DefaultValue(true)> _ <Bindable(True)> _ <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _ Public Shadows Property Checked() As Boolean Get Return MyBase.Checked End Get Set(ByVal value As Boolean) MyBase.Checked = value End Set End Property Private Sub updateNullState() If NullDate and Not DesignMode Then MyBase.CustomFormat = " " MyBase.Format = DateTimePickerFormat.Custom Else MyBase.CustomFormat = CustomFormatBacking MyBase.Format = FormatBacking End If End Sub Public Sub BeginInit() Implements System.ComponentModel.ISupportInitialize.BeginInit End Sub Public Sub EndInit() Implements System.ComponentModel.ISupportInitialize.EndInit updateNullState() End Sub Protected Overrides Sub OnValueChanged(ByVal eventargs As System.EventArgs) MyBase.OnValueChanged(eventargs) updateNullState() End Sub End Class 
0
source

I had the same problem. Well, actually I'm smart enough to understand, but my users had a problem.

I decided to remove the checkbox and add 2 radio buttons. Now it looks like this:

(using pseudo UI)

 O No value entered O | 1/1/2010 |V| 

The upper radio is checked when there is no value (null), the lower value when there is a value. I am not hiding or disabling the bottom control, and users seem to understand.

The disadvantage is that this requires much more space.

PS: The next thing users will complain about is to use the scroll wheel when there is focus in the combo box.

+1
source

Klugey, but he seems to have done his job. If unchecked, a null value is allowed.

 Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged If DateTimePicker1.Checked Then DateTimePicker1.Format = DateTimePickerFormat.Short 'Or whatever the original format was Else DateTimePicker1.Format = DateTimePickerFormat.Custom DateTimePicker1.CustomFormat = " " End If End Sub 

OK, the next question ... How do I flip this behavior in a subclass of DateTimePicker? I want to do this to fix the initial values ​​of the Format and CustomFormat properties set in the Properties window. But this is clearly not a way to do this.

Here is my weak attempt:

 Public Class NullableDateTimePicker Inherits DateTimePicker Private _OriginalFormat As DateTimePickerFormat Private _OriginalCustomerFormat As String Private Sub NullableDateTimePicker_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ValueChanged If Me.Checked Then Me.Format = _OriginalFormat Me.CustomFormat = _OriginalCustomerFormat Else Me.Format = DateTimePickerFormat.Custom Me.CustomFormat = " " End If End Sub Private Sub _DP_FormatChanged(ByVal sender As Object, ByVal e As System.EventArgs) Static Count As Integer If Count = 0 Then _OriginalFormat = Me.Format _OriginalCustomerFormat = Me.CustomFormat End If Count += 1 End Sub Public Sub New() AddHandler MyBase.FormatChanged, AddressOf _DP_FormatChanged End Sub End Class 
+1
source

I understand that this is already many years after your initial question, but here is a subclass of Telerik RadDateTimePicker that does what you ask for:

 Imports Telerik.WinControls.UI Public Class DateTimePickerWithNull Inherits Telerik.WinControls.UI.RadDateTimePicker Private ReadOnly _calendar As RadCalendar Sub New() Dim calendarBehavior As RadDateTimePickerCalendar = Me.DateTimePickerElement.GetCurrentBehavior() calendarBehavior.DropDownMinSize = New Size(220, 150) _calendar = calendarBehavior.Calendar _calendar.ShowFooter = True AddHandler _calendar.ClearButton.Click, AddressOf ClearButton_Click AddHandler _calendar.TodayButton.Click, AddressOf TodayButton_Click End Sub Private Sub ClearButton_Click(sender As Object, e As EventArgs) 'Do this to put the calendar away _calendar.SelectedDate = _calendar.FocusedDate 'Then clear Me.SetToNullValue() End Sub Private Sub TodayButton_Click(sender As Object, e As EventArgs) _calendar.SelectedDate = _calendar.FocusedDate End Sub End Class 

To get the value of the collector:

 If DateTimePicker1.Value.Date = DateTimePicker1.NullDate Then Label1.Text = "Null" Else Label1.Text = DateTimePicker1.Value.ToLongDateString End If 
+1
source

All Articles