Forum Discussion

MulgraveTester's avatar
MulgraveTester
Frequent Contributor
8 years ago
Solved

Navigate to Hours section of a DateTimePicker time field

I want to set the time of a DateTimePicker using a similar method to what a user would enter. i.e. I want to use the keys method and I don't want to set the wTime property programmatically.

 

The problem is that I do not know which element (Hours, Minutes or Meridian) will have focus when I tab to this field so I can't just tab to the field and key 3[Right]45[Right]AM because the minutes or meridian may have focus when I click or tab the field. The help assumes that the Hours element has focus when the field gains focus.

 

Is there a shortcut key or method that lets me jump to the first element of this control or a property that tells me which element has focus?

  • Colin_McCrae's avatar
    Colin_McCrae
    8 years ago

    Hmmm.

     

    I don't have one to hand to play about with.

     

    But ....

     

    You can set focus to it. So you know it is the active control.

     

    You can get it's height/length. (And also relative co-ordinates to the screen and parent control if required)

     

    So you *could* apply a mouse click around 5 pixels in from the left edge of the control (at half height)? It appears to be left aligned, so that would cause it to always move focus to the hours part I would think? At 5 pixels, I don't think there is any danger of going too far right and ending up in the minutes section instead.

     

    And as you have all the sizes and relative co-ordinates, you can make use of these so that it tracks it if it moves or the application is opened in a different area of the screen.

     

    I know using co-ordinates doesn't sound ideal. But in some cases (like this) it's OK as long as you control it properly and have known parameters to act as anchors and you do your co-ordinate based clicks relative to these.

     

    I have to do it for a few custom controls myself. Mainly as that's how the controls are built! (eg. A tree with checkboxes that has an event trigger which is activated by a mouseclick on the tree object where the checkbox is positioned! Crazy, but that's how they did it, so that's how I have to do it ...)

10 Replies

  • baxatob's avatar
    baxatob
    Community Hero

     


    MulgraveTester wrote:

    The help assumes that the Hours element has focus when the field gains focus.



    That is true, you should get the first block (Hours) selected by default after getting your dateTimePicker into the focus.

     

    You can use

     

    dateTimePicker.SetFocus()

     

    and then

     

    dateTimePicker.Keys()

    • MulgraveTester's avatar
      MulgraveTester
      Frequent Contributor

      The first element (Hours) is NOT selected by default when the field is given focus. If the last element edited was Minutes and I use the setfocus method the field gains focus but the Minutes element is select (not Hours). As I cannot jump to the first element or know where I have arrived in the field I cannot process it reliably.

      • Colin_McCrae's avatar
        Colin_McCrae
        Community Hero

        Hmmm.

         

        I don't have one to hand to play about with.

         

        But ....

         

        You can set focus to it. So you know it is the active control.

         

        You can get it's height/length. (And also relative co-ordinates to the screen and parent control if required)

         

        So you *could* apply a mouse click around 5 pixels in from the left edge of the control (at half height)? It appears to be left aligned, so that would cause it to always move focus to the hours part I would think? At 5 pixels, I don't think there is any danger of going too far right and ending up in the minutes section instead.

         

        And as you have all the sizes and relative co-ordinates, you can make use of these so that it tracks it if it moves or the application is opened in a different area of the screen.

         

        I know using co-ordinates doesn't sound ideal. But in some cases (like this) it's OK as long as you control it properly and have known parameters to act as anchors and you do your co-ordinate based clicks relative to these.

         

        I have to do it for a few custom controls myself. Mainly as that's how the controls are built! (eg. A tree with checkboxes that has an event trigger which is activated by a mouseclick on the tree object where the checkbox is positioned! Crazy, but that's how they did it, so that's how I have to do it ...)