Xenobiologist

[SOLVED] Input - type time (format -> 00:00) MY_WM_COMMAND function?

6 posts in this topic

#1 ·  Posted (edited)

Hi,

I have some input controls which I use to get a "time" value from the user.

Something like this: Started work at : 08:54

I've found this function :

Func MY_WM_COMMAND($hWnd, $msg, $wParam, $lParam)
    Local $backspc = 0, $temp
    Local $nNotifyCode = _WinAPI_HiWord($wParam)
    Local $nID = _WinAPI_LoWord($wParam)
    Local $hCtrl = $lParam
    Switch $nID ; aktuelle Inputbox
        Case $kommen_I Or $gehen_I Or $arbeitszeit_I Or $mittagsPause_I
            Switch $nNotifyCode
                Case $EN_CHANGE
                    $temp = StringSplit(GUICtrlRead($nID), ":")
                    If @error Then
                        _ArrayAdd($temp, "")
                    EndIf
                    $temp[1] = StringReplace($temp[1], " ", "")
                    If $temp[0] = 1 Then
                        $temp[1] = StringLeft($temp[1], 1) & " "
                        GUICtrlSetData($nID, $temp[1] & ":")
                        Send("{LEFT 2}")
                    EndIf

                    Switch StringLen($temp[2])
                        Case 2
;~                             Send("{TAB}")
                        Case 1
                            If Not StringIsDigit($temp[1]) Then
                                GUICtrlSetData($nID, "  :")
                                Send("{LEFT 3}")
                            EndIf
                            If $temp[2] > 5 Then
                                $temp[2] = ""
                                GUICtrlSetData($nID, $temp[1] & ":" & $temp[2])
                            EndIf
                        Case 0
                            Switch StringLen($temp[1])
                                Case 2
                                    Switch StringLeft($temp[1], 1)
                                        Case 2
                                            If $temp[1] > 23 Then
                                                $temp[1] = "2 "
                                                GUICtrlSetData($nID, $temp[1] & ":")
                                                Send("{LEFT 2}")
                                            Else
                                                GUICtrlSetData($nID, $temp[1] & ":")
                                            EndIf
                                        Case 1
                                            GUICtrlSetData($nID, $temp[1] & ":")
                                        Case 0
                                            GUICtrlSetData($nID, $temp[1] & ":")
                                    EndSwitch
                                Case 1
                                    Switch $temp[1]
                                        Case 3 To 9
                                            $temp[1] = "0" & $temp[1]
                                            GUICtrlSetData($nID, $temp[1] & ":")
                                        Case 0 To 2
                                            GUICtrlSetData($nID, $temp[1] & " :")
                                            Send("{LEFT 2}")
                                    EndSwitch
                                Case 0
                                    GUICtrlSetData($nID, "  :")
                                    Send("{LEFT 3}")
                            EndSwitch
                    EndSwitch


                    If StringLeft(GUICtrlRead($nID), 1) > 2 Then
                        GUICtrlSetData($nID, '0' & GUICtrlRead($nID))
                        ContinueCase
                    EndIf

                Case $EN_SETFOCUS
                    GUICtrlSetState($nID, $GUI_FOCUS)
                Case $EN_KILLFOCUS
                    $temp = StringSplit(GUICtrlRead($nID), ":")
                    $temp[1] = StringReplace($temp[1], " ", "")
                    If $temp[0] = 1 Then _ArrayAdd($temp, "")
                    If StringLen($temp[2]) = 2 Then
                        If StringLen($temp[1]) = 2 Then
                            Return
                        Else
                            GUICtrlSetData($nID, "  :")
                            Return
                        EndIf
                    EndIf
                    Switch StringIsDigit($temp[1])
                        Case True
                            Switch StringLen($temp[1])
                                Case 1
                                    If $temp[1] > 2 Then $temp[1] = "0" & $temp[1]
                                    If $temp[1] <= 2 Then $temp[1] = "  "
                                Case 2
                                    If $temp[2] > "" Then
                                        $temp[2] = $temp[2] & "0"
                                        GUICtrlSetData($nID, $temp[1] & ":" & $temp[2])
                                    EndIf
                            EndSwitch
                            If $temp[2] = "" Or $temp[2] = 0 Then
                                $temp[2] = "00"
                                GUICtrlSetData($nID, $temp[1] & ":" & $temp[2])
                            EndIf
                        Case False
                            GUICtrlSetData($nID, "  :")
                    EndSwitch
            EndSwitch
    EndSwitch
EndFunc   ;==>MY_WM_COMMAND

Anybody who has done this before with other code? I think, I can write it on my own, but maybe someone has done it before.

The function needs to do the following:

  • 24h format - If first digit is > 2, lets say 8 then write 08:
  • user types in only the digits, the : is inserted automatically
  • values from 00:00 to 23:59 are possible - 12:77 should work.
    • If the users types in 12:7 then the input should just show 12:

Thanks for any suggestions.

Mega

 

 

Edited by Xenobiologist

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hi, Mega. Seems like a lot just to calculate start time. Are you looking for the end user to input their own, or looking to grab the current time and input it for them? Either way, this is a quick and dirty just using _NowTime() with 24-hour format; defaults to the current time, but the user could overwrite with their own. (I don't understand why you say 12:77 should work?)

#include <Date.au3>
#include <GUIConstantsEx.au3>

$aTime = StringSplit(_NowTime(4), ":")

$hGUI = GUICreate("Test", 300, 100)
GUISetFont(24, 600, Default, "Arial")
GUICtrlCreateLabel(":", 150, 10, 20, 30)
$startHour = GUICtrlCreateInput($aTime[1], 30, 10, 100, 40)
$startMin = GUICtrlCreateInput($aTime[2], 170, 10, 100, 40)

    GUISetState(@SW_SHOW)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
        EndSwitch
    WEnd

For any calculation you would just do something like this:

$startTime = GUICtrlRead($startHour) & ":" & GUICtrlRead($startMin)

Or am I oversimplifying what you're asking?

Edited by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Not sure this will fit all the requirements but... who knows...  :)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>

Opt("GUIOnEventMode", 1)

$hGUI = GUICreate("test", 232, 90)
$Label = GUICtrlCreateLabel("Enter the time", 22, 8, 163, 17)
$input = GUICtrlCreateInput("", 22, 30, 165, 24)
GUICtrlSetLimit(-1, 5)
GUICtrlSetFont(-1, 12)
GUISetState()

GUIRegisterMsg($WM_COMMAND, '_WM_COMMAND')
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

While Sleep(100)
WEnd

Func _Exit()
    Exit
EndFunc

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
 Local $id = BitAND($wParam, 0x0000FFFF)
 Local $code = BitShift($wParam, 16)
   If $id = $input AND $code = $EN_UPDATE Then
        GUICtrlSetData($input, _FormatInputNumber(GUICtrlRead($input)))
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc

Func _FormatInputNumber($iNumber)
   If StringRegExp($iNumber, '^:|[^:\d]|:.*:') Then Return StringTrimRight($iNumber, 1) 
   Local $tmp = StringSplit($iNumber, ":")
 Switch $tmp[0]
   Case 1
     ; If first digit is > 2, lets say 8 then write 08:
     If StringLeft($tmp[1], 1) > 2 Then $tmp[1] = "0" & $tmp[1] & ":"
     ; values from 00:00 to 23:59 are possible
     If $tmp[1] > 23 Then Return StringTrimRight($iNumber, 1) 
     ; user types in only the digits, the : is inserted automatically
     If StringLen($tmp[1]) = 2 Then $tmp[1] &= ":"
     If StringLen($tmp[1]) > 3 Then Return "don't fool me !"
     Return $tmp[1]
   Case 2 
     If StringLen($tmp[1]) < 2 Then $iNumber = "0" & $tmp[1] & ":"
     ; If the users types in 12:7 then the input should just show 12:
     If StringLeft($tmp[2], 1) > 5 Then Return StringTrimRight($iNumber, 1) 
     Return $iNumber 
 EndSwitch
EndFunc

 

Edited by mikell

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Thanks to both of you.

@JLogan3o13: it is more ... how to create a function which lets the user as easy as possible type in a time in a specific format.

Later on I use that for a little tool which lets you type in your start time at work and some other stuff and then reminds you when you can go home or when you reach the 10 hours.

In Germany it is not allowed to work more than 10 hours without a good reason.

@mikell: Pretty good.

Only one thing I found. When you have e.g. 08:54 you cannot go back before the colon. At 08: the backspace doesn't work anymore.

Then you need to doubleclick so that everything is selected.

 

Edited by Xenobiologist

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

Only one thing I found. When you have e.g. 08:54 you cannot go back before the colon. At 08: the backspace doesn't work anymore.

This should do the trick  :)

Func _FormatInputNumber($iNumber)
   If _IsPressed("08") Then Return $iNumber
   If StringRegExp($iNumber, '^:|[^:\d]|:.*:') Then Return StringTrimRight($iNumber, 1) 
   ; etc

 

Share this post


Link to post
Share on other sites

Thanks man, seems to work like a charm.

Should be added to scriplets thread.

Validating an input for getting a time value is something that is used by many scripts, I suppose.


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now