DateTimePicker Bug?

C

Chris Fairman

NG,

Maybe I'm missing a key step, but it seems when typing dates into a
DateTimePicker control (format = 'Short'), the controls' .text property is
not updated until the control losses focus.

I'm handling an <enter> key event on a DateTimePicker control.
Essentially I want to give a user the ability to type a date in the
DTP, and press <enter> and have a DataGrid populate with user
supplied data from the DTP control.

I can get the DataGrid to populate, but the DTP control isn't updated
with the key strokes supplied by the user, unless:
1. The control looses focus
2. You click into another field within the control itself. (If you were
changing the 'day' value of the control, clicking in the month or year
fields will update the 'day' value in the .text property.)

So...
If the DTP control reads [02/15/2005] and I click on the 15, type 20 and
press <enter> the .text value for the DTP control still reads 02/15/2005,
instead of 02/20/2005. Unless... (as stated above -control losses focus,
etc...)

I've recreated the DTP controls just in case, but getting the same
behavior.

I've tried invoking both .update() and .refresh() DTP methods
before reading in the .text value, but none of these methods are helping.
The .text value and the actual text of the DTP are never equivalent
(Unless -as stated above -control losses focus, etc...)

Hope all that made sense to somebody. :)

Thanks in Advance,
-Chris
 
O

Oenone

Chris said:
Maybe I'm missing a key step, but it seems when typing dates into a
DateTimePicker control (format = 'Short'), the controls' .text
property is not updated until the control losses focus.

I've tried and tried to recreate this, but I haven't been able to. Every
time I query it, it returns the newly-entered date.

I added a DateTimePicker control to a form and changed its Format to Short.

I then first tried adding a command button and set it as the AcceptButton
for the form. In the click event I put this code:

MsgBox(DateTimePicker1.Text & " : " & DateTimePicker1.Value)

No matter what I did, the correct date was always displayed when I pressed
Enter.

I then took away the command button and put the same MsgBox into the
DatePicker's KeyDown event for when the user pressed Enter:

If e.KeyCode = Keys.Enter Then
MsgBox(DateTimePicker1.Text & " : " & DateTimePicker1.Value)
e.Handled = True
End If

This worked fine too.

Are you handling the enter key differently in your project? Can you recreate
the problem in a standalone project?

The only thing I found similar to your description was if I typed a
single-digit value into a two digit field. For example, if I opened the form
defaulting to today's date, and typed just "2" into the month field, the
MsgBox would still display 2005-04-07 (though the display in the control now
correctly showed February). OK-ing the MsgBox and pressing Enter again
returned the expected date of 2005-02-07. If I type a two-digit day or
month, this doesn't occur though.

I found I could work around this by simply telling the form to set focus to
itself before the MsgBox statement (Me.Focus). This then produced the
correct date in the MsgBox, and still left the focus in the datepicker
control. Perhaps you might be able to try this in your code?
 
C

Chris Fairman

Thank you very much for your almost immediate response. Your quite
right,
it seems this behavior is only exhibited when entering partial days,
months or years.

I guess I failed to notice that. All my test cases where with single
digits (even though my posted example used double digits -sorry!)...

Programmaticly changing focus to the form, and back to the object
works,
but that seems like an ugly workaround for what might be considered
expected/basic behavior.

This along with how the cursor is not moved to a day or year after
typing in a month or day makes me think the dot-net DTP could be a bit
more polished.

I guess I'm stuck with it, maybe they will spruce it up for dot-net
v.2.

Anyhow thank you for taking the time to look at it, moving the focus
around to other objects and back again makes it behave like I was
hoping
it would 'out of the box'.

-Chris
 

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

Similar Threads


Top