שתף באמצעות


DateTimePicker control... Tab Between Hrs, Mins etc (VS 2008)

Question

Thursday, February 23, 2017 2:06 PM

Hi,

I'm trying to allow for tabbing between elements (HH:MM) of the DateTimePicker control (using VB.Net 2008) instead of using the  out-of-the-box functionality using the right and left arrow keys.   I've done a lot of reading on this and so far this is a complete fail.   it seems the control won't allow for this functionality no matter how the thing is configured.

The frustrating part is that, on my WIN7 desktop, if I 'Change the Date and Time Settings' function, the DateTime control on THAT OS - generated form allows this.

Is Microsoft using a third-party control there?   Or is there something I'm missing?

Any clue is appreciated.

Thanks,

Tom

All replies (8)

Friday, February 24, 2017 3:08 AM ✅Answered | 1 vote

 You can create your own DateTimePicker class which inherits from the DateTimePicker class.  Then use the ProcessCmdKey overides function to handle the special command keys such as the Tab and Shft+Tab.  When the Tab or Shft+Tab keys are detected,  use SendKeys.Send to send a Left or Right arrow key message to the focused control which will be the DateTimePicker.  Just Return True to indicate that you have handled the keys and that the Tab and Shft+Tab keys should not be processed any further.

 Add the class below and Build your application.  Then you will find the "MyDateTimePicker" control in your ToolBox.  Replace your DateTimePicker with one of the "MyDateTimePicker" controls.  You will then have the functionality you want.

Public Class MyDateTimePicker
    Inherits DateTimePicker

    Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
        If keyData = Keys.Tab Then
            SendKeys.Send("{RIGHT}")
            Return True
        End If
        If keyData = (Keys.Shift Or Keys.Tab) Then
            SendKeys.Send("{LEFT}")
            Return True
        End If
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function
End Class

 

 Here i am pressing Tab and Shft+Tab to go backward and forward.  The button is just to show that the focus does not shift to the button when Tab or Shft+Tab is pressed.

If you say it can`t be done then i`ll try it


Friday, February 24, 2017 1:58 PM ✅Answered

So simple that it's brilliant.   Thanks so much for the help.

Tom


Friday, February 24, 2017 2:23 AM

Hi Tom,

This forum is discussing Visual Studio WPF/SL Designer, Visual Studio Guidance Automation Toolkit, Developer Documentation and Help System, and Visual Studio Editor.

Your issue is related to VB develop, I will move this thread to corresponding forum for a professional answer.

Sincerely,

Oscar

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


Friday, February 24, 2017 3:03 AM

Hi,

I'm trying to allow for tabbing between elements (HH:MM) of the DateTimePicker control (using VB.Net 2008) instead of using the  out-of-the-box functionality using the right and left arrow keys.   I've done a lot of reading on this and so far this is a complete fail.   it seems the control won't allow for this functionality no matter how the thing is configured.

The frustrating part is that, on my WIN7 desktop, if I 'Change the Date and Time Settings' function, the DateTime control on THAT OS - generated form allows this.

Is Microsoft using a third-party control there?   Or is there something I'm missing?

Any clue is appreciated.

Thanks,

Tom

Hi

As you have found, the control doesn't support what you are asking for.

Here is some code that may be a suitable alternative. Although it uses 3 DateTimePicker controls, the end result is easily used, and, might be a better way to handle HH:mm changes too.

' Form1 with DateTimePicker1,
' DateTimePicker2, DateTimePicker3
' Label1
Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        DateTimePicker1.Format = DateTimePickerFormat.Custom
        DateTimePicker1.CustomFormat = "ddd, dd MMM yyyy"

        DateTimePicker2.Format = DateTimePickerFormat.Custom
        DateTimePicker2.CustomFormat = "HH"
        DateTimePicker2.ShowUpDown = True

        DateTimePicker3.Format = DateTimePickerFormat.Custom
        DateTimePicker3.CustomFormat = "mm"
        DateTimePicker3.ShowUpDown = True

        DateTimePicker1.Value = Now
        DateTimePicker2.Value = Now
        DateTimePicker3.Value = Now

        DateTimePicker1.Select()

    End Sub

    Private Sub DateTimePicker_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged, DateTimePicker2.ValueChanged, DateTimePicker3.ValueChanged
        Label1.Text = DateTimePicker1.Value.Date.ToString("ddd, dd MMM yyyy") & DateTimePicker2.Value.ToString(" HH:") & DateTimePicker3.Value.ToString("mm")
    End Sub
End Class

Regards Les, Livingston, Scotland


Friday, February 24, 2017 1:58 PM

Thanks Oscar...


Friday, February 24, 2017 2:08 PM

Les, 

This is actually a solution that I had considered... knew it would work.  Thanks for the input. 

Check out what was submitted by IronRarez, below.  Only thing I needed to add to that is a way to get to the next control in tab-sequence which was easy enough to do by over-riding the ENTER key. 

Tom


Friday, February 24, 2017 2:11 PM

So simple that it's brilliant.   Thanks so much for the help.

Tom

Tom,

You should mark Razerz's post as the answer as it has the answer.

:)


Friday, February 24, 2017 2:13 PM

Thought I had... thanks for the reminder