zuladabef Posted October 7, 2020 Posted October 7, 2020 I would like to enter a time of day and have another input produce the difference in hours based upon the previous input, without having to press an update button. So in this example here, $iDifferenceInHours = $defaultTimeOut - $iTimeIn I would like the user to input the Time In and then whenever the user moves away from the $iTimeIn input that the $iDifferenceInHours would find the difference between $defaultTimeOut and $iTimeIn, but how does the GUI know how to do it on the event that the user moves away from the $iTimeIn input? Hope that makes sense. #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 343, 225, 192, 124) $iTimeIn = GUICtrlCreateInput("", 150, 32, 121, 21) $Label1 = GUICtrlCreateLabel("Time In", 88, 32, 39, 17) $defaultTimeOut = GUICtrlCreateInput("15:00", 150, 72, 121, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_READONLY)) $Label2 = GUICtrlCreateLabel("Default Time Out", 40, 72, 84, 17) $iDifferenceInHours = GUICtrlCreateInput("", 150, 120, 121, 21) $Label3 = GUICtrlCreateLabel("Difference", 75, 120, 53, 17) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd
seadoggie01 Posted October 7, 2020 Posted October 7, 2020 When the user presses Enter or Tab while inside of the input or if they click off of the input, it fires an event IF there was a change. (Add a "Case $iTimeIn") All my code provided is Public Domain... but it may not work. Use it, change it, break it, whatever you want. Spoiler My Humble Contributions:Personal Function Documentation - A personal HelpFile for your functionsAcro.au3 UDF - Automating Acrobat ProToDo Finder - Find #ToDo: lines in your scriptsUI-SimpleWrappers UDF - Use UI Automation more Simply-erKeePass UDF - Automate KeePass, a password managerInputBoxes - Simple Input boxes for various variable types
Subz Posted October 7, 2020 Posted October 7, 2020 (edited) Another option, needs tidying up: #include <Date.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> Global $g_sTime = "15:00" #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 343, 225, 192, 124) $iTimeIn = GUICtrlCreateInput($g_sTime, 150, 32, 121, 21) $Label1 = GUICtrlCreateLabel("Time In", 88, 32, 39, 17) $defaultTimeOut = GUICtrlCreateInput("15:00", 150, 72, 121, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_READONLY)) $Label2 = GUICtrlCreateLabel("Default Time Out", 40, 72, 84, 17) $iDifferenceInHours = GUICtrlCreateInput("", 150, 120, 121, 21) $Label3 = GUICtrlCreateLabel("Difference", 75, 120, 53, 17) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### AdlibRegister("_UpdateTime") While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Func _UpdateTime() Local $sTime = GUICtrlRead($iTimeIn) If $sTime = $g_sTime Then Return GUICtrlSetData($iDifferenceInHours, _DateDiff("h", _NowCalcDate() & " " & $sTime, _NowCalcDate() & " " & GUICtrlRead($defaultTimeOut))) $g_sTime = $sTime EndFunc Edited October 8, 2020 by Subz Updated _DateDiff to show difference in hours not minutes.
pixelsearch Posted October 7, 2020 Posted October 7, 2020 (edited) I would have done it registering WM_COMMAND() message, testing $EN_KILLFOCUS (Sent when control loses the keyboard focus) on the appropriate input control Edit: as seadoggie01 & Subz solutions work fine too, no need to register Edited October 7, 2020 by pixelsearch "I think you are searching a bug where there is no bug... don't listen to bad advice."
zuladabef Posted October 8, 2020 Author Posted October 8, 2020 Thank you to everyone! I did not know about AdlibRegister. That is clever. 18 hours ago, Subz said: Another option, needs tidying up: #include <Date.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> Global $g_sTime = "15:00" #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 343, 225, 192, 124) $iTimeIn = GUICtrlCreateInput($g_sTime, 150, 32, 121, 21) $Label1 = GUICtrlCreateLabel("Time In", 88, 32, 39, 17) $defaultTimeOut = GUICtrlCreateInput("15:00", 150, 72, 121, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_READONLY)) $Label2 = GUICtrlCreateLabel("Default Time Out", 40, 72, 84, 17) $iDifferenceInHours = GUICtrlCreateInput("", 150, 120, 121, 21) $Label3 = GUICtrlCreateLabel("Difference", 75, 120, 53, 17) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### AdlibRegister("_UpdateTime") While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Func _UpdateTime() Local $sTime = GUICtrlRead($iTimeIn) If $sTime = $g_sTime Then Return GUICtrlSetData($iDifferenceInHours, _DateDiff("n", _NowCalcDate() & " " & $sTime, _NowCalcDate() & " " & GUICtrlRead($defaultTimeOut))) $g_sTime = $sTime EndFunc
zuladabef Posted November 6, 2020 Author Posted November 6, 2020 What if I want the time to automatically round up to the next 15 minutes? I tried something like this, but it's not working for some reason. Func _UpdateTime() Local $sTime = GUICtrlRead($inputBegTimeOfAbs) Local $aTimeSplit = StringSplit($sTime, ":") Local $iStartHour = $aTimeSplit[1] Local $iMinutesOnly = $aTimeSplit[2] Switch $iMinutesOnly Case 1 To 14 $MinutesOnly = 15 Case 16 To 29 $iMinutesOnly = 30 Case 31 To 44 $iMinutesOnly = 45 Case 46 To 59 $iMinutesOnly = 00 $iStartHour = $iStartHour + 1 EndSwitch GUICtrlSetData($inputBegTimeOfAbs, $iStartHour & ":" & $iMinutesOnly) Local $g_sTime = GUICtrlRead($defaultTimeOut) If $sTime = $g_sTime Then Return GUICtrlSetData($inputNumOfHoursRequested, (_DateDiff("n", _NowCalcDate() & " " & $sTime, _NowCalcDate() & " " & GUICtrlRead($defaultTimeOut)) / 60)) $g_sTime = $sTime EndFunc ;==>_UpdateTime
zuladabef Posted November 6, 2020 Author Posted November 6, 2020 (edited) Sorry for the double post, not sure how to delete this duplicate. Edited November 6, 2020 by zuladabef
Subz Posted November 6, 2020 Posted November 6, 2020 Maybe something like: expandcollapse popup#include <Array.au3> #include <Date.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> Global $g_sTime = "15:00" #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 343, 225, 192, 124) $iTimeIn = GUICtrlCreateInput($g_sTime, 150, 32, 121, 21) $Label1 = GUICtrlCreateLabel("Time In", 88, 32, 39, 17) $defaultTimeOut = GUICtrlCreateInput("15:00", 150, 72, 121, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_READONLY)) $Label2 = GUICtrlCreateLabel("Default Time Out", 40, 72, 84, 17) $iDifferenceInHours = GUICtrlCreateInput("", 150, 120, 121, 21) $Label3 = GUICtrlCreateLabel("Difference", 75, 120, 53, 17) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### AdlibRegister("_UpdateTime") While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Func _UpdateTime() Local $sTime = GUICtrlRead($iTimeIn) If StringLen($sTime) < 5 Then Return If $sTime = $g_sTime Then Return Local $aTimeSplit = StringSplit($sTime, ":") Switch $aTimeSplit[2] Case 1 To 14 $aTimeSplit[2] = 15 Case 16 To 29 $aTimeSplit[2] = 30 Case 31 To 44 $aTimeSplit[2] = 45 Case 46 To 59 $aTimeSplit[2] = 00 $aTimeSplit[1] += 1 EndSwitch If $aTimeSplit[1] > 23 Then $aTimeSplit[1] = "00" $sTime = $aTimeSplit[1] & ":" & $aTimeSplit[2] GUICtrlSetData($iTimeIn, $sTime) GUICtrlSetData($iDifferenceInHours, _DateDiff("n", _NowCalcDate() & " " & $sTime, _NowCalcDate() & " " & GUICtrlRead($defaultTimeOut))) $g_sTime = $sTime EndFunc
GokAy Posted November 6, 2020 Posted November 6, 2020 (edited) Hey, If you want it to be more flexible, perhaps write the algorithm like this. Seems better than using select case. Convert to AutoIt, as I have quickly calculated in Excel. ; Considering we have equal steps as in 15 mins vs 60 mins $iMinuteRoundTo = 15 $iModValue = 60/$iMinuteRoundTo ; (60/15) in this case $iCurrentMinute = whatever minutes you want to check $iMinuteInterval = $iMinuteRoundTo * MOD(RoundUp(($iCurrentMinute/$iMinuteRoundTo),0),$iModValue) ; And this would give $iStartHour $iStartHour = RoundDown((RoundUp(($iCurrentMinute/$iMinuteRoundTo),0)/$iModValue),0) Edited November 7, 2020 by GokAy Added calculation for $iStartHour
zuladabef Posted November 9, 2020 Author Posted November 9, 2020 This is just what I was looking for, thanks! I did add a StringFormat to make sure that 0 reads 00. Switch $aTimeSplit[2] Case 1 To 14 $aTimeSplit[2] = 15 Case 16 To 29 $aTimeSplit[2] = 30 Case 31 To 44 $aTimeSplit[2] = 45 Case 46 To 59 $aTimeSplit[2] = StringFormat("00", 00) $aTimeSplit[1] += 1 EndSwitch On 11/6/2020 at 2:31 PM, Subz said: Maybe something like: expandcollapse popup#include <Array.au3> #include <Date.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> Global $g_sTime = "15:00" #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 343, 225, 192, 124) $iTimeIn = GUICtrlCreateInput($g_sTime, 150, 32, 121, 21) $Label1 = GUICtrlCreateLabel("Time In", 88, 32, 39, 17) $defaultTimeOut = GUICtrlCreateInput("15:00", 150, 72, 121, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_READONLY)) $Label2 = GUICtrlCreateLabel("Default Time Out", 40, 72, 84, 17) $iDifferenceInHours = GUICtrlCreateInput("", 150, 120, 121, 21) $Label3 = GUICtrlCreateLabel("Difference", 75, 120, 53, 17) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### AdlibRegister("_UpdateTime") While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Func _UpdateTime() Local $sTime = GUICtrlRead($iTimeIn) If StringLen($sTime) < 5 Then Return If $sTime = $g_sTime Then Return Local $aTimeSplit = StringSplit($sTime, ":") Switch $aTimeSplit[2] Case 1 To 14 $aTimeSplit[2] = 15 Case 16 To 29 $aTimeSplit[2] = 30 Case 31 To 44 $aTimeSplit[2] = 45 Case 46 To 59 $aTimeSplit[2] = 00 $aTimeSplit[1] += 1 EndSwitch If $aTimeSplit[1] > 23 Then $aTimeSplit[1] = "00" $sTime = $aTimeSplit[1] & ":" & $aTimeSplit[2] GUICtrlSetData($iTimeIn, $sTime) GUICtrlSetData($iDifferenceInHours, _DateDiff("n", _NowCalcDate() & " " & $sTime, _NowCalcDate() & " " & GUICtrlRead($defaultTimeOut))) $g_sTime = $sTime EndFunc
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now