5 Replies - 1636 Views - Last Post: 09 April 2013 - 06:56 AM Rate Topic: -----

#1 skilly2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 01-March 13

Select partial text on receiving focus for DateTimePicker

Posted 08 April 2013 - 06:07 AM

I have a DateTimePicker with a custom format of "hh:mm tt", and a ShowUpDown property of true. When the control receives focus, the selection defaults to the hh portion of the contents. When you hit the up or down buttons of the DateTimePicker, it increases/decreases the hour part of the time accordingly. If you click on the minute section, I can use the up down buttons to increase/decrease the minutes. I would like to somehow start off the selection in the minutes section when the control receives focus, rather than the hours section. I've searched on how to do this, but I have been unable to turn up anything. Any ideas?

Thanks,
Kevin

Is This A Good Question/Topic? 0
  • +

Replies To: Select partial text on receiving focus for DateTimePicker

#2 C.Andrews  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 166
  • Joined: 18-October 12

Re: Select partial text on receiving focus for DateTimePicker

Posted 08 April 2013 - 06:46 AM

This may be a dirty hack of a solution, but you could add a line to the GotFocus handler for your date-time picker that arrows right once:

    Private Sub DateTimePicker1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateTimePicker1.GotFocus
        SendKeys.Send("{Right}")
    End Sub



There's almost certainly a better way to do it.

This post has been edited by C.Andrews: 08 April 2013 - 06:48 AM

Was This Post Helpful? 0
  • +
  • -

#3 skilly2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 01-March 13

Re: Select partial text on receiving focus for DateTimePicker

Posted 08 April 2013 - 06:55 AM

View PostC.Andrews, on 08 April 2013 - 06:46 AM, said:

This may be a dirty hack of a solution, but you could add a line to the GotFocus handler for your date-time picker that arrows right once:

    Private Sub DateTimePicker1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateTimePicker1.GotFocus
        SendKeys.Send("{Right}")
    End Sub



There's almost certainly a better way to do it.


Good enough for now. If I come across a better solution, I will post it. There is one problem with doing it this way (not a big enough deal for me to worry about right now). The first time the control receives focus it works. The control must remember where the selction was when it receives the focus next time, as the second time it receives focus it selects the AM/PM part of the text. Like I said, not a big enough deal for me to worry about.

Thanks for your help!

Kevin
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Select partial text on receiving focus for DateTimePicker

Posted 08 April 2013 - 07:45 AM

I found this on VBForums from kleinma and added teh sendkey to it. You would add this to your project and build. Then in the toolbox, you should see DateTimePickerEx. Drag that and set up the format the way you want it. When you enter it refreshes the control and sends the right key to the control. Which should leave you hilighting minutes with each focus.
Public Class DateTimePickerEx
    Inherits System.Windows.Forms.DateTimePicker

    Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)
        'GET THE CURRENT FORMAT AND CUSTOMFORMAT STRING OF THE DTP
        Dim CurrentFormat As DateTimePickerFormat = Me.Format
        Dim CurrentCustomFormat As String = Me.CustomFormat

        'IF THE FORMAT IS NOT CUSTOM, CHANGE IT TO CUSTOM
        'OTHERWISE CHANGE IT TO SOMETHING OTHER THAN CUSTOM
        If Me.Format <> DateTimePickerFormat.Custom Then
            Me.Format = DateTimePickerFormat.Custom
            'SET THE CUSTOM FORMAT TO AN EMPTY STRING
            Me.CustomFormat = ""
        Else
            Me.Format = DateTimePickerFormat.Short
        End If

        'SET BACK THE CACHED VALUES SO THE ACTUAL FORMAT NEVER REALLY CHANGES
        Me.Format = CurrentFormat
        Me.CustomFormat = CurrentCustomFormat

        MyBase.OnEnter(e)
        SendKeys.Send("{Right}")
    End Sub
End Class

Was This Post Helpful? 1
  • +
  • -

#5 skilly2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 01-March 13

Re: Select partial text on receiving focus for DateTimePicker

Posted 09 April 2013 - 06:24 AM

View PostCharlieMay, on 08 April 2013 - 07:45 AM, said:

I found this on VBForums from kleinma and added teh sendkey to it. You would add this to your project and build. Then in the toolbox, you should see DateTimePickerEx. Drag that and set up the format the way you want it. When you enter it refreshes the control and sends the right key to the control. Which should leave you hilighting minutes with each focus.
Public Class DateTimePickerEx
    Inherits System.Windows.Forms.DateTimePicker

    Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)
        'GET THE CURRENT FORMAT AND CUSTOMFORMAT STRING OF THE DTP
        Dim CurrentFormat As DateTimePickerFormat = Me.Format
        Dim CurrentCustomFormat As String = Me.CustomFormat

        'IF THE FORMAT IS NOT CUSTOM, CHANGE IT TO CUSTOM
        'OTHERWISE CHANGE IT TO SOMETHING OTHER THAN CUSTOM
        If Me.Format <> DateTimePickerFormat.Custom Then
            Me.Format = DateTimePickerFormat.Custom
            'SET THE CUSTOM FORMAT TO AN EMPTY STRING
            Me.CustomFormat = ""
        Else
            Me.Format = DateTimePickerFormat.Short
        End If

        'SET BACK THE CACHED VALUES SO THE ACTUAL FORMAT NEVER REALLY CHANGES
        Me.Format = CurrentFormat
        Me.CustomFormat = CurrentCustomFormat

        MyBase.OnEnter(e)
        SendKeys.Send("{Right}")
    End Sub
End Class



Thanks CharlieMay! I added the class and overrode the regular DateTimePicker. It works great. I'm trying to understand why it works though. Can you explain what MyBase.OnEnter(e) does? I know what the rest of the code does (even though I'm not sure why we're changing the format, and then changing it back). I would like to understand the code if you could help explain it to me. Someday I'd like to be able to override controls on my own, and I'm getting a step closer to understanding.

Thank you so much for your help!
Was This Post Helpful? 0
  • +
  • -

#6 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Select partial text on receiving focus for DateTimePicker

Posted 09 April 2013 - 06:56 AM

MyBase effectively overrides the override. In this case, a call to the OnEnter event would normally have used the "closest" one (the one in this code). MyBase forces it to use the OnEnter in the base class.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1