DateTimePicker.Value

M

mahesh.nimbalkar

Hi,

I have DateTimePicker control on a winform. When user selects a date
in that control, I am getting proper value using DateTimePicker.Value.
But I want to know in case user does not select any date and closes
DateTimePicker control by pressing escape key. Here in this case also
I am getting today's date even if user did not select any date in that
control.

I should get null or something else that will let me know that user
did not select anything and closed the DateTimePicker by pressing
escape key.

Any idea how to do that?

Thanks!
 
G

Guest

You can set the date to DateTime.MinValue
instead of today's date.

You can identify the chagne by using the events

ValueChanged
and
ChangeUICues

to identify the change.

-VR
 
V

VisualHint

Hello Mahesh,

If the user does not select a date and presses Esc, then you get the
current value (the one that was shown before showing the drop down
calendar) when you call Value. This is normal and what is expected.

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
 
M

mahesh.nimbalkar

Thanks for the reply.

User can select what is shown in the control while displaying that
control. So I can not come to know that whether you has selected the
date or pressed escape key.

Thanks
 
M

mahesh.nimbalkar

Thanks for the reply.

The user can select whatever date shown in the control so I can not
come to know whether user has really selected any value or pressed
escape key because in both cases I will get same value.
 
V

VisualHint

I'm not sure to understand what you mean.
As another poster added, when the user selects a date you will get a
ValueChanged event. When he hits ESC you won't. This should allow you
to discriminate both cases.
You have also the CloseUp event so that you know when the dropdown is
closed.

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
 
M

mahesh.nimbalkar

I already tried ValueChanged event. But the problem with that is when
user opens DateTimePicker control and closes it by pressing escape,
even in this case also ValueChanged event gets fired.
 
V

VisualHint

Indeed youre right. I just tried it. In fact you are partially right.
Until I modify the displayed date, ValueChanged will be triggered even
if ESC is hit. After a change it's not trigerred anymore on ESC. But
anyway, if the user types the arrow keys to move the cue in the month
calendar, ValueChanged is triggered and hitting ESC after would not
help. At least this allows you to see how this native control is
illogical. I am fixing all these oddities in Smart FieldPackEditor.

Anyway, maybe you should do things differently. What is your exact
goal ? If you want to know that ESC has been pressed, why not try to
detect the fact that ESC is actually typed instead of relying on the
Value property which won't never be null anyway...

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
 
M

mahesh.nimbalkar

On some event I am showing DateTimePicker control in open state. Now
if users selected date, I would do some processing and user presses
ESC, I would just ignore it.

I main point here is that I just wanted to know whether user has
selected any date or not. Because on that only I will be processing
some data.

I tried key press events also, but seems that if control is in open
state, it will not fire any keyboard events.

If you have any alternate solution to know if user has selected any
date in control, please let me know. I am stuck now.

Thanks!
 
V

VisualHint

When showing the picker in open state, why don't you record the
initial date? When the CloseUp event occurs you would simply compare
the old date with the one returned by Value... Not enough?

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
 
M

mahesh.nimbalkar

Yes it will work except one case and that is not acceptable. The case
is, user can select whatever is the default date shown in the
DateTimePicker control and here I will not come to know that user has
actually selected a date and not pressed ESC key if I compare default
date with currently selected date.
 
V

VisualHint

What you want to do is not so easy and maybe you should rethink the
way you do it (showing the datetimepicker opened initially is not very
intuitive and consistent with the OS after all). In Smart PropertyGrid
I was using the MS DateTimePicker and I gave up because I was not able
to achieve the same level of quality and usability than the other
editors. At the end, I had reached a point where I had some message,
keyboard and mouse hooks on the calendar popup and even then there was
always something not satisfying. This has been the trigger to start a
complete DateTimePicker replacement and I think this will be a real
success.

If you still want to go on this path, try to get the handle of the
calendar and hook various messages so that you know what happens with
the user interaction.

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
 
M

mahesh.nimbalkar

Al right. Can you please post some code sample how to hook key pressed
event for this DateTimePicker control?
 
V

VisualHint

In the DropDown event handler:

hMonthCalendar = (IntPtr)SendMessage(dateTimePicker.Handle, 0x1008 /
*DTM_GETMONTHCAL*/, 0, 0);

You will find on CodeProject several articles about hooks. Here is
one: http://www.codeproject.com/csharp/globalhook.asp (don't know if
it's the best).

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
 
M

mahesh.nimbalkar

Thank you!


In the DropDown event handler:

hMonthCalendar = (IntPtr)SendMessage(dateTimePicker.Handle, 0x1008 /
*DTM_GETMONTHCAL*/, 0, 0);

You will find on CodeProject several articles about hooks. Here is
one:http://www.codeproject.com/csharp/globalhook.asp(don't know if
it's the best).

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top