Results 1 to 11 of 11

Thread: [RESOLVED!] DTPicker and Focus

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2000
    Location
    argentina
    Posts
    25

    [RESOLVED!] DTPicker and Focus

    Hi people
    I always use this forum to search for things when i got stuck in some error, but here i have an issue that i cannot find in here
    here's the thing:
    When i modify, ie, the month part in a dtpicker, and go to another control, if i go back to the dtpicker, the focus is still in the month part, but the dataentrys for the system i am doin', when they go again to the dtpicker, want the focus always be in the day part of it.. i have no clue how to do that, but i think it has to be an easy way to do that

    well.. i hope that u can understand my problem since i'm from Argentina and english is not my primary language

    thanks,
    Ricardo
    Last edited by stockton; Sep 23rd, 2005 at 01:39 PM. Reason: resolved

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: DTPicker and Focus

    hmm.. Ive tried a number of things and cant get it to do it!??
    always wants to be at the month part.. (UNless u click on it)

    even tried SendKeys chr$(39) which is the right arrow key
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: DTPicker and Focus

    You can use SendKeys "{LEFT}" (or {RIGHT}) to move the focus to a different date part. Place the code in the GotFocus event.

    Unfortunately there is no way to know which date part currently has input focus. You will need to keep track of that manually - unless there is some API functionality that is not implemented in VB but is accessible via SendMessage or something...

  4. #4
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: DTPicker and Focus

    lol.. i forgot about "{RIGHT}" when testing sendkeys
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: DTPicker and Focus

    Try this
    VB Code:
    1. Private testdate As String
    2.  
    3. Private Sub DTPickter1_Click()
    4. testdate = DTPickter1.Value
    5. End Sub
    6.  
    7.  
    8. Private Sub dtpAppealLtrDT_GotFocus()
    9. 'need this On Error because the first time it is clicked during
    10. 'a session the variable will be empty, and therefore, will generate
    11. 'an error
    12. On Error Resume Next  
    13. DTPickter1.Value = testdate
    14. End Sub

  6. #6

    Thread Starter
    Junior Member
    Join Date
    May 2000
    Location
    argentina
    Posts
    25

    Re: DTPicker and Focus

    thanks for the replies..
    i will try how to use SendKeys "{LEFT}", but i think it will be kind of hard to code something that works
    in the other hand, i don't understand the code by Hack, if u can explain me a little how that work it will be excelent

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: DTPicker and Focus

    When you pick a date, it stores that date in a variable.

    The next time you open the datepickter, it takes what is in that variable for a date and makes it the default date of the datepickter which I believe is what you wanted.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    May 2000
    Location
    argentina
    Posts
    25

    Re: DTPicker and Focus

    Quote Originally Posted by Hack
    When you pick a date, it stores that date in a variable.

    The next time you open the datepickter, it takes what is in that variable for a date and makes it the default date of the datepickter which I believe is what you wanted.
    nop, the thing i want is: when the dtpicker got the focus, always the focus be on the day part... that's why, in example, if you change the year part, go to another control, and go back to the dtpicker, the focus will be in the year part of the dtpicker (the last part you changed) instead of being in the day part

  9. #9
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: DTPicker and Focus

    Sometimes we try to make things too complicated. After thinking about the problem some more I came up with a simple solution.

    VB Code:
    1. Private Sub DTPicker1_GotFocus()
    2.     'this code moves the focus to the Day input section
    3.     'when the date picker control gets focus
    4.     'Assumes the date picker is in mm/dd/yyyy format
    5.    
    6.     Dim dteTemp As Date
    7.    
    8.     With DTPicker1
    9.         'store the current value because it will be modified.
    10.         dteTemp = .Value
    11.    
    12.         'Force a change to the current date section
    13.         SendKeys "{UP}", True
    14.    
    15.         If Year(dteTemp) <> .Year Then
    16.             'the UP key affected the Year section, so move Left to the Day section.
    17.             SendKeys "{LEFT}"
    18.         ElseIf Month(dteTemp) <> .Month Then
    19.             'the UP key affected the Month section, so move Right to the Day section.
    20.             SendKeys "{RIGHT}"
    21.         End If
    22.    
    23.         'restore the current value.
    24.         .Value = dteTemp
    25.     End With
    26.    
    27. End Sub

    The only problem is that this code causes the Change and several KeyDown events to occur, so you may need to "know" when this code is executing. Set the mblnDTPGotFocus variable to True at the beginning of the GotFocus event and back to False at the end.

    VB Code:
    1. 'form level variable
    2. Private mblnDTPGotFocus As Boolean
    3.  
    4. Private Sub DTPicker1_Change()
    5.     If Not mblnDTPGotFocus Then
    6.         'the event is firing because of a user change, not the GotFocus event.
    7.         Debug.Print "DTPicker1_Change"
    8.     End If
    9. End Sub

  10. #10

    Thread Starter
    Junior Member
    Join Date
    May 2000
    Location
    argentina
    Posts
    25

    Re: DTPicker and Focus

    Great Bruce !! it worked nice ! very clean and understandable

    thank you very much !

  11. #11
    Lively Member
    Join Date
    Sep 2005
    Posts
    82

    Re: [RESOLVED!] DTPicker and Focus

    Thanks Bruce,

    I also was trying to resolve the same prob., but can't and last with left it as it is.
    now I know this will solve my prob.

    Sanjay

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width