Solved

DTPicker

Posted on 2000-04-07
20
1,808 Views
Last Modified: 2013-12-25
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!
0
Comment
Question by:scrumper64
  • 6
  • 5
  • 4
  • +2
20 Comments
 

Expert Comment

by:mcb123
Comment Utility
scrumper64:

Use the following after the setfocus stmt:

    SendKeys "{TAB}"

Hope this helps.
0
 

Author Comment

by:scrumper64
Comment Utility
Doesn't work, I need to move within the DTPicker.
0
 

Expert Comment

by:mcb123
Comment Utility
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}"

0
 

Author Comment

by:scrumper64
Comment Utility
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!
0
 
LVL 14

Expert Comment

by:wsh2
Comment Utility
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).

0
 
LVL 14

Expert Comment

by:wsh2
Comment Utility
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.
0
 

Expert Comment

by:mcb123
Comment Utility
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)".
0
 
LVL 14

Expert Comment

by:wsh2
Comment Utility
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?
0
 

Expert Comment

by:mcb123
Comment Utility
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?
0
 
LVL 14

Expert Comment

by:wsh2
Comment Utility
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>.



0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
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.  =)

0
 
LVL 14

Expert Comment

by:wsh2
Comment Utility
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>.

0
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
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.    =)
0
 
LVL 28

Accepted Solution

by:
AzraSound earned 100 total points
Comment Utility
ok so maybe I was THAT bored. i was determined to do it all in vb and i think i got it.  


Dim pos As String
Dim tempDate(0 To 2) As Integer


Private Sub DTPicker1_GotFocus()
    SendKeys "{UP}"
    DoEvents
    If DTPicker1.Day <> tempDate(0) Then _
        pos = "day"
    If DTPicker1.Month <> tempDate(1) Then _
        pos = "month"
    If DTPicker1.Year <> tempDate(2) Then _
        pos = "year"
    Text1.Text = tempDate(0)
    Text2.Text = tempDate(1)
    Text3.Text = tempDate(2)
    SendKeys "{DOWN}"
    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_LostFocus()
    tempDate(0) = DTPicker1.Day
    tempDate(1) = DTPicker1.Month
    tempDate(2) = DTPicker1.Year
End Sub

Private Sub Form_Load()
    pos = "month"
    tempDate(0) = DTPicker1.Day
    tempDate(1) = DTPicker1.Month
    tempDate(2) = DTPicker1.Year
End Sub
0
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
dont mind those text.text = tempDate statements.  i was just using those to test the code visually.
0
 

Author Comment

by:scrumper64
Comment Utility
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.
0
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
I usually dont want to strain my brain ever but that night was an exception for some odd reason.
0
 
LVL 3

Expert Comment

by:myqlG
Comment Utility
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
0
 

Author Comment

by:scrumper64
Comment Utility
Have some points mate, you deserve them!
I guess this is the only way to close a Question Thread.
0
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
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. =)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now