DateTimePicker seems slow to catch up

J Trahair

Well-known member
Joined
May 14, 2008
Messages
175
Location
Spain
Programming Experience
10+
The DateTimePicker seems to be behaving irrationally. I have a normal form in VB2005. The datetimepicker value is set on entry to 01 March 2009. I set the value to 01 January 2009 by typing 1 in the 'March' area. I then use a keyboard shortcut to click a command button, eg. Alt-I. I noticed the DateTimePicker was late to update as the command button sequence started at 1 March 2009, not 1 January 2009. So I inserted the following line of code:
HTML:
    Private Sub DateTimePicker4_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker4.ValueChanged

        Me.Text = DateTimePicker4.Value

    End Sub
so when the value changed, the form text value would display it. Then, at the beginning of the command button process I show
HTML:
MsgBox(DateTimePicker4.Value, MsgBoxStyle.Information, "")
which displays the March date value.
Please see screenshot.
Is there a way of ensuring the DateTimePicker value is always correct?
 

Attachments

  • DateTimePicker.jpg
    DateTimePicker.jpg
    59.4 KB · Views: 25
You have probably noticed that the value in DTP only changes when you move to next field or out of control. Calling the button mnemonic doesn't focus the button so the DTP also won't get triggered until the dialog returns, you are retrieving this value before showing the dialog that changes the focus. If you didn't show the dialog here, but instead displayed the value by other means you would see the month part would remain active. What you can do is to force the change of active control before getting the value, for example Select the button in its Click event. You can select the DTP again after the updated value is retrieved if necessary.
 
...as in
HTML:
Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click

        Button11.Focus()    'Ensures DateTimePicker4 is updated!!!!

        Call PrintSpanishInvoices()

    End Sub
where Button11 is the one the mnemonic refers to...?

Is Microsoft phasing out keyboard shortcuts??!

Thanks for your help.
 
Use the Select method, not the Focus method, as explained in help.
Is Microsoft phasing out keyboard shortcuts??!
er...what? mnemomics (access keys) and keyboard shortcuts are two different things.
 
Back
Top