Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

DTPicker

Posted on 2000-04-07
20
Medium Priority
?
2,037 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
ID: 2693774
scrumper64:

Use the following after the setfocus stmt:

    SendKeys "{TAB}"

Hope this helps.
0
 

Author Comment

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

Expert Comment

by:mcb123
ID: 2693907
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:scrumper64
ID: 2693966
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
ID: 2694105
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
ID: 2694404
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
ID: 2694449
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
ID: 2694507
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
ID: 2694740
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
ID: 2694775
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
 
LVL 28

Expert Comment

by:AzraSound
ID: 2697372
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
ID: 2697428
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
ID: 2697460
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 300 total points
ID: 2697502
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
ID: 2697504
dont mind those text.text = tempDate statements.  i was just using those to test the code visually.
0
 

Author Comment

by:scrumper64
ID: 2699525
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
ID: 2702885
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
ID: 2717828
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
ID: 2735002
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
ID: 2735058
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

885 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