Link to home
Start Free TrialLog in
Avatar of PSCTECH
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:
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then SendKeys.Send("{TAB}")

Open in new window

until I am moving from a textbox to a datetimepicker.  

When the datetimepicker gets focus i use
SendKeys.Send("%{DOWN}")

Open in new window

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.
Avatar of John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou
Flag of Greece image

Hi ...
For the Etner Code you can use the Key_Down Event
Something like ...
 Private Sub DateTimePicker1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles DateTimePicker1.KeyDown
        If Me.DateTimePicker1.Focus Then
            If e.KeyCode = Keys.Return Then
                SendKeys.Send("%{DOWN}")
            End If
        End If
    End Sub

Open in new window

Hope it hleps...

John
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
Private Sub DateTimePicker1_Enter(sender As System.Object, e As System.EventArgs) Handles DateTimePicker1.Enter
        SendKeys.Send("%{DOWN}")
    End Sub

Open in new window

That means also that you have you to manage the Key_Down Event of your Previous focused Control
that had the Focus using :
If e.KeyCode = Keys.Return Or e.KeyCode = Keys.Tab Then
            DateTimePicker1.Focus()
        End If

Open in new window

Avatar of PSCTECH
PSCTECH

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)
4.  DateTimePicker1 opens and immediately closes
It 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

Open in new window

Avatar of PSCTECH

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
Avatar of PSCTECH

ASKER

if it helps here is the code I'm using

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

Open in new window


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

Open in new window

Yes  PSCTECH i noticed...trying
Avatar of PSCTECH

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...
Avatar of Mike Tomlinson
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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PSCTECH

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!