DateTimePicker - how to show nothing when user has not specified a date?

  • Thread starter Thread starter Richard Lewis Haggard
  • Start date Start date
R

Richard Lewis Haggard

Is there some way to have the DateTimePicker display nothing? I came up with
a hack but I was hoping that there was a less offensive method than the
horrid little code snippet that fell out of my fingers when I wasn't paying
attention.

Here's why I need to have the date time picker be able to not display date
information. My client wants to display nothing if the underlying data model
does not have a datetime value. If the user sets focus in the control then
the client wants the date time picker to immediately light up with a default
date time. If the user leaves the control without entering a value then it
goes blank again. Otherwise, it hangs on to the user's entered value from
then on.

Here's the hack. When the form is first shown, display the date time picker
if the data model's date time is valid. Otherwise, hide the date time picker
and display a text box where the date time picker would have been. When text
box receives focus, hide the text box, show the date time picker and
transfer the focus to the date time picker. On leaving the date time picker,
hide the date time picker and display the text box if the user did not enter
in a value. If the user did specify a value then leave the date time picker
visible and the text box remains hidden.

Most of this horrid little code snippet works just fine (or at least as well
as one might hope for). However, there is a problem with the text box's tab
index. It is set with the same value as the date time picker's but it
insists on being someplace very different in the tabbing order. Why is this
and how can I fix it?

A second little puzzle has to do with the date time picker's Leave event
handler being called in response to calling dateTimePicker.Focus(). Why is
the Leave event being called? How do I stop this?
 
TabIndex - a control's tab order is not responsive to setting its TabIndex.
Instead, it can be set through the control parent's
Controls.SetChildIndex( childControl, newIndex ) method.

DateTimePicker gets Leave event notification on dateTimePicker.SetFocus() -
As for the date time picker getting a Leave event notification after focus
was programmatically set to the control, I never did figure that out. I got
around the problem by simply adding a bool that was set when the code set
focus to the date time control. The Leave event looked at the bool and said,
"Ah. This is a spurious event. I do not need to make sure that the text box
is on top of me. Oh yes, the bool must be cleared, too."
 
Hello Richard,

the DateTimePicker class and underlying win32 object are not flexible
at all. That's why I created Smart FieldPackEditor.Net, an editor
capable of editing rule-based fields. The DateTimePicker is an example
(I just added another one which is the IP address editor). I rewrote
it from scratch and if you want to give it a try you will get the
flexibility you want. It is currently in beta and you can play with
it. There is a sample and many screencasts to see it in action.

Best regards,

Nicolas Cadilhac @ VisualHint (http://www.visualhint.com)
Home of Smart FieldPackEditor.Net / DateTimePicker replacement
Home of Smart PropertyGrid for .Net and MFC
Microsoft PropertyGrid Resource List - http://www.propertygridresourcelist.com
 
Back
Top