Link to home
Start Free TrialLog in
Avatar of scrumper64
scrumper64

asked on

DTPicker

Does anyone know how to Set Focus to the Day part of the DTPicker? If you tab onto it you always start on the Day, Month or Year part that you were in last time.
I did it with:
    dtpDate.SetFocus
    SendKeys "%{DOWN}{ESC}"
but this is no use because some DTPicker's are disabled (on a different Tab), plus it make them flash.
I ask this because some users of our application don't use a mouse and it is annoying for them.
Respects to any responders!
Avatar of mcb123
mcb123

scrumper64:

Use the following after the setfocus stmt:

    SendKeys "{TAB}"

Hope this helps.
Avatar of scrumper64

ASKER

Doesn't work, I need to move within the DTPicker.
Hi scrumper64:

I was trying to send you a revised answer.  the first one was based on DTP being the only control in the form!!

Here's the correct solution:
    dtpDate.SetFocus
    SendKeys "{RIGHT}"

Sorry mate, that keeps moving from day, to month, to year and back, I need to go to day each time.
It's a tricky one isn't it, thanks for trying!
If dtpDate.Updown = True Then

Private Sub DTPicker1_GotFocus()
  SendKeys "{RIGHT}"
End Sub

moves the highlight to the Day (when using a mm/dd/yyyy format).

Ya know something.. this one is a p*sser. To get around it.. takes quite a bit of code.. (Change, KeyPress and GotFocus Events).. as the questioner states.. there has to be an easier way.. <smile> and a <groan>.. LOL.
I have tried to make my last code fail, but everytime it goes straight to the day portion of the dtpicker control.  I have 2 text boxes and one dtp.  I have put the two lines in form load and it works everytime!!!

I try different things at every run - change month, set focus to year, or go to one of the text boxes and yet everytime I rerun the program, I get to the right spot - the day portion of the dtp control.

Maybe it has to do with some code in your form?  Could you give me some more info on your form?

Also - I would suggest checking the version of the DTP control you are running.  I am running "Date Time Picker ActiveX Control - Version 6.0 (SP3)".
In my case.. I am using "Microsoft Windows Common Controls-2 6.0 (SP3)".. with 3 Date Time Pickers. My results are the same as scrumper64.. with the highlight going from box to box. mcb123.. where did your control come from?
WSH2 - I have the one you mentioned.  U mentioned 3 DTPs'.  So couldn't u setfocus on the one you need to deal with at that time and then use the sendkeys code with IT?
Ahhh.. if it were that simple.. <sigh>.
So far.. No.  

Whenever I go into the Date Picker control and change the date.. and then Tab out of it.. and then Tab back into it.. it will go to the mm or dd or yy that is to the right of the one last entered. Using the GetFocus event, I can issue a sendkeys and it seems to work.. but the problem is.. knowing where you are at.

To that end, I then coded around the DateTimePicker Change event, to pick up the last mm dd or yy changed, store it, and then use it for the next GetFocus event. But then I realized the Change event does not fire if the arrow keys are used to move from mm dd yy.. which then throws all the positioning out. As I ran out of time.. I did not go the extra step to then trap keypresses. Realizing that this whole thing is a mess, I called it quits.. <smile> and a <wink>.

All I gotta say is.. Scrumper64.. Eff the Date Time Picker.. GO TO COMBOBOXES !!!! <lol> and a <wink>.



well if your users dont have mouses (mice?) then you can implement the code this way since their only option is to use the arrow keys correct???

Dim pos As String


Private Sub DTPicker1_GotFocus()
    Select Case pos
        Case "month"
            SendKeys "{RIGHT}"
        Case "day"
            'do nothing, you're there
        Case "year"
            SendKeys "{RIGHT}{RIGHT}"
    End Select
End Sub

Private Sub DTPicker1_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyRight Then
        Select Case pos
            Case "month"
                pos = "day"
            Case "day"
                pos = "year"
            Case "year"
                pos = "month"
        End Select
    End If
    If KeyCode = vbKeyLeft Then
        Select Case pos
            Case "month"
                pos = "year"
            Case "day"
                pos = "month"
            Case "year"
                pos = "day"
        End Select
     End If
End Sub

Private Sub Form_Load()
    pos = "month"
End Sub



Obviously create another string variable to maintain other dtpicker controls.  I dont even know if this is what youre asking but I'm bored and felt like coding.  =)

Hello Azra.. Bored?.. <huge smile>
Believe it or not.. this IS a tough one.. one that I don't believe can be solved by VB alone.. <sheesh>. So what do you do when VB fails?.. You got it.. API's.. <lol>.. !!!

For fun, I bounced this one off of Ark. As he does NOT have VB6 (ie the DateTimePicker Control) he couldn't do much.. but suggested the following:

Ark Writes:
"Unfortunatelly, I'm using VB5 and so haven't DTP control and can not play with it. But I think it may be done with this way:

1. Use GetWindow(DTPHwnd, GW_CHILD),GetWindow(DTPHwnd, GW_NEXT) in a Loop with GetClassName - and find Edit class window with its handle.

2. Subclass this window and wait for EN_SETFOCUS or EN_SELCHANGE or same messages

3. Send EM_SETSEL or EM_SCROLLCARRET to this window"

As you are bored and I don't like the DateTimePicker very much.. perhaps you might want to play with this. As they say, "Pain is GOOD".. <lol> and a <wink>.

it doesnt appear the dtpicker understands those constants as i believe they are normally used in conjunction with the richtextbox control.  However this is what i have so far using the subclassing and timer assistant (ssubtmr.dll)


Private Const WM_COMMAND = &H111
Private Const EN_SETFOCUS = &H100
Private Const EM_SETSEL = &HB1

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Implements ISubclass

Private Sub Form_Load()
    AttachMessage Me, DTPicker1.hwnd, WM_COMMAND
End Sub


Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
   DetachMessage Me, DTPicker1.hwnd, WM_COMMAND
End Sub


Private Property Let ISubclass_MsgResponse(ByVal RHS As SSubTimer.EMsgResponse)
   '
End Property

Private Property Get ISubclass_MsgResponse() As SSubTimer.EMsgResponse
   ISubclass_MsgResponse = emrPreprocess
End Property

Private Function ISubclass_WindowProc(ByVal hwnd As Long, ByVal iMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
   Dim ID As Long
   Dim iCode As Long
   Select Case iMsg
   Case WM_COMMAND
      If lParam <> 0 Then
         iCode = (wParam And &HFFFF0000) \ &H10000
         ' the id of controls created by VB is the index
         ' of the control in the form's control array plus 1
         ID = wParam And &HFFFF&
         If TypeOf Controls(ID - 1) Is DTPicker Then
            Select Case iCode
            Case EN_SETFOCUS
                SendMessage DTPicker1.hwnd, EM_SETSEL, 4, 5
            End Select
         End If
      End If
   End Select
End Function



the problem is the SendMessage call with EM_SETSEL is supposed to send, as parameters, the starting character position and ending character position of the selection range (e.g. 4 and 5).  I had no idea what to put here and I don't think the dtpicker will even recognize that anyways.  Ok, I'm not that bored, I too am calling it quits on this one.    =)
ASKER CERTIFIED SOLUTION
Avatar of AzraSound
AzraSound
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
dont mind those text.text = tempDate statements.  i was just using those to test the code visually.
I've found a "bodge" that seems to work OK.  On each GotFocus I do the following:

dtpDate.Format = 1
dtpDate.Format = 3
dtpDate.CustomFormat = "dd MMM yyyy"

Impressive stuff though AzraSound!  I was thinking of trying something like that myself, but didn't want to strain my brain late on a Friday.
I usually dont want to strain my brain ever but that night was an exception for some odd reason.
Please dont use Sendkeys..its a bug
rittled POS. Use Keybd_event API
instead.  Let me know if you need some
help on it..I'd be glad to paste some code
Have some points mate, you deserve them!
I guess this is the only way to close a Question Thread.
actually you can just let questions die out...i think after 30 days they just go away if they havent been answered.  just lettin' you know for future reference.  thanks for the points though. =)