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.
.NET ProgrammingVisual Basic.NET

Avatar of undefined
Last Comment
PSCTECH

8/22/2022 - Mon
John (Yiannis) Toutountzoglou

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
John (Yiannis) Toutountzoglou

Is this what you want or you want directly to open the DatetimePicker while got focus?
John (Yiannis) Toutountzoglou

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

Your help has saved me hundreds of hours of internet surfing.
fblack61
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)
John (Yiannis) Toutountzoglou

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

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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
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

John (Yiannis) Toutountzoglou

Yes  PSCTECH i noticed...trying
PSCTECH

ASKER
I really appreciate the help.  It's been driving me crazy for 2 days.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
John (Yiannis) Toutountzoglou

Somehow the enter key fires the CloseUP event...i can not figure out what is happening...still try
John (Yiannis) Toutountzoglou

i try also instead of SendKeys.Send("%{DOWN}") to use   SendKeys.Send("{F4}") but also no luck...
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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Mike Tomlinson

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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!