PSCTECH
asked on
vb.net datetimepicker behavior
I have an app that runs on a tablet. The virtual keyboard is a pain to use so I am trying to accomplish as much as possible by using the limited buttons (up, down, left and right arrows and Enter button) on the exterior of the tablet.
I want to allow the user to move through the controls on the form by pressing the Enter button (this is an actual button not a virtual keyboard key). This is working fine using the KeyPress event and:
When the datetimepicker gets focus i use
What I would like to happen is for the user to press the Enter button and the "Tab" is sent and the datetimepicker gets focus, is for the control to open and remain open until a date is chosen.
I can simulate all of this behavior on my pc. Using the Enter Key produces the same problematic behavior. Using the actual Tab Key produces the desired behavior. But again the keyboard on the device is not a good option.
I want to allow the user to move through the controls on the form by pressing the Enter button (this is an actual button not a virtual keyboard key). This is working fine using the KeyPress event and:
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then SendKeys.Send("{TAB}")
until I am moving from a textbox to a datetimepicker. When the datetimepicker gets focus i use
SendKeys.Send("%{DOWN}")
the calendar opens, immediately closes and the month (of the current date) which is set at form load is highlighted.What I would like to happen is for the user to press the Enter button and the "Tab" is sent and the datetimepicker gets focus, is for the control to open and remain open until a date is chosen.
I can simulate all of this behavior on my pc. Using the Enter Key produces the same problematic behavior. Using the actual Tab Key produces the desired behavior. But again the keyboard on the device is not a good option.
Is this what you want or you want directly to open the DatetimePicker while got focus?
In the second way you could use Enter Event
that had the Focus using :
Private Sub DateTimePicker1_Enter(sender As System.Object, e As System.EventArgs) Handles DateTimePicker1.Enter
SendKeys.Send("%{DOWN}")
End Sub
That means also that you have you to manage the Key_Down Event of your Previous focused Controlthat had the Focus using :
If e.KeyCode = Keys.Return Or e.KeyCode = Keys.Tab Then
DateTimePicker1.Focus()
End If
ASKER
Both of those methods are still producing the problematic behavior.
I know my original post was a little unclear.
Here's whats happening:
1. Textbox 1 has focus
2. User presses Enter button
3. DateTimePicker1 gets focus
4. DateTimePicker1 opens and immediately closes
5. Month part of text in DateTimePicker1 is highlighted
Here's what I want to happen:
1. Textbox 1 has focus
2. User presses Enter button
3. DateTimePicker1 gets focus and opens (stays open until user interaction)
I know my original post was a little unclear.
Here's whats happening:
1. Textbox 1 has focus
2. User presses Enter button
3. DateTimePicker1 gets focus
4. DateTimePicker1 opens and immediately closes
5. Month part of text in DateTimePicker1 is highlighted
Here's what I want to happen:
1. Textbox 1 has focus
2. User presses Enter button
3. DateTimePicker1 gets focus and opens (stays open until user interaction)
4. DateTimePicker1 opens and immediately closesIt does not Closes immediately unless you click somewhere else..
Personally i can not find a way to make the picker remains open
A user interaction causes the Values Changed event to fire
So a trick is to set a boolean Variable to false and turn it to true when a value change.
if not in lost Focus you can turn again the focus to datetimePicker ..But this cause the control to open again waiting for a selection ...
somthing like ..
Private isSelected As Boolean = False
Private Sub DateTimePicker1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles DateTimePicker1.KeyDown
If e.KeyCode = Keys.Return Or e.KeyCode = Keys.Tab And Not isSelected Then
DateTimePicker1.Focus()
End If
End Sub
Private Sub DateTimePicker1_Enter(sender As System.Object, e As System.EventArgs) Handles DateTimePicker1.Enter
SendKeys.Send("%{DOWN}")
End Sub
Private Sub DateTimePicker1_ValueChanged(sender As System.Object, e As System.EventArgs) Handles DateTimePicker1.ValueChanged
isSelected = True
End Sub
Private Sub DateTimePicker1_LostFocus(sender As System.Object, e As System.EventArgs) Handles DateTimePicker1.LostFocus
If Not isSelected Then
Me.DateTimePicker1.Focus()
End If
End Sub
Private Sub DateTimePicker1_GotFocus(sender As System.Object, e As System.EventArgs) Handles DateTimePicker1.GotFocus
SendKeys.Send("%{DOWN}")
End Sub
ASKER
Again, I'm not being clear. Even on my laptop, I get the behavior below:
On my form
if the user presses Enter: This is the problem
1. Textbox 1 has focus
2. User presses Enter button
3. DateTimePicker1 gets focus
4. DateTimePicker1 opens and immediately closes
5. Month part of text in DateTimePicker1 is highlighted
if the user presses Tab: This is the desired behavior
1. Textbox 1 has focus
2. User presses Tab button
3. DateTimePicker1 gets focus, opens and stays open
I want it to stay open whether they press Enter or Tab
On my form
if the user presses Enter: This is the problem
1. Textbox 1 has focus
2. User presses Enter button
3. DateTimePicker1 gets focus
4. DateTimePicker1 opens and immediately closes
5. Month part of text in DateTimePicker1 is highlighted
if the user presses Tab: This is the desired behavior
1. Textbox 1 has focus
2. User presses Tab button
3. DateTimePicker1 gets focus, opens and stays open
I want it to stay open whether they press Enter or Tab
ASKER
if it helps here is the code I'm using
I have also tried
Private Sub txtDate_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDate.KeyDown
If e.KeyCode = Keys.Return Or e.KeyCode = Keys.Tab Then DateTimePicker1.Focus()
End Sub
Private Sub DateTimePicker1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateTimePicker1.Enter
SendKeys.Send("%{DOWN}")
End Sub
I have also tried
Private Sub txtDate_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDate.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then SendTab()
End Sub
Private Sub DateTimePicker1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateTimePicker1.GotFocus
SendKeys.Send("%{DOWN}")
End Sub
Yes PSCTECH i noticed...trying
ASKER
I really appreciate the help. It's been driving me crazy for 2 days.
Somehow the enter key fires the CloseUP event...i can not figure out what is happening...still try
i try also instead of SendKeys.Send("%{DOWN}") to use SendKeys.Send("{F4}") but also no luck...
This works for me in WinForms...
Public Class Form1
Private Const WM_KEYUP As Integer = &H101
Private Sub txtDate_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDate.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
e.Handled = True ' Suppress the bloody beep!
End If
End Sub
Protected Overrides Function ProcessKeyPreview(ByRef m As System.Windows.Forms.Message) As Boolean
Select Case m.Msg
Case WM_KEYUP
Select Case m.WParam.ToInt32
Case Keys.Enter
If Me.ActiveControl Is txtDate Then
DateTimePicker1.Focus()
SendKeys.Send("{F4}")
End If
End Select
End Select
Return MyBase.ProcessKeyPreview(m)
End Function
Private Sub DateTimePicker1_CloseUp(sender As Object, e As System.EventArgs) Handles DateTimePicker1.CloseUp
SendKeys.Send("{TAB}")
End Sub
End Class
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Perfect in every way. I couldn't have asked for a better experience from an expert! Listened to my question, tested and solved. A+++. THANK YOU VERY MUCH!
For the Etner Code you can use the Key_Down Event
Something like ...
Open in new window
Hope it hleps...John