icu Posted June 22, 2011 Share Posted June 22, 2011 (edited) Dear AutoIt Community,Please help. I'm at my wit's end. I'm trying to handle logic errors when someone enters dates that my program won't like.I have two calendars on my GUI: $To_Date and $From_Date.I need to have a MsgBox pop up to say something like "You've entered bad dates" for the following situations:$To_Date < _NowCalc()$From_Date < _NowCalc()$From_Date > $To_DateHowever I just can't seem to get it working. I've experimented a lot and I'm using the forum as a last resort. The latest code is below:expandcollapse popup#include <Date.au3> #include <ButtonConstants.au3> #include <DateTimeConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> Func _NowDateDayMonthYear() Local $i_Month = @MON Local $i_Day = @MDAY Local $i_Year = @YEAR Local $s_Month_Long_Name = _DateToMonth($i_Month) Local $s_Now_Date = $i_Day & " " & $s_Month_Long_Name & " " & $i_Year Return $s_Now_Date EndFunc #Region ### START Koda GUI section ### Form= $Form_Date_Test = GUICreate("Form2", 413, 96, 233, 476) $Label_From_Date = GUICtrlCreateLabel("From Date:", 8, 24, 59, 17) $From_Date = GUICtrlCreateDate(_NowCalc(), 72, 16, 121, 25) $Label_To_Date = GUICtrlCreateLabel("To Date:", 224, 24, 46, 17) $To_Date = GUICtrlCreateDate(_DateAdd('M', 1, _NowCalc()), 280, 16, 121, 25) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() ; Date buffers $From_Date_Buffer = GUICtrlRead($From_Date) $To_Date_Buffer = GUICtrlRead($To_Date) Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $From_Date Select Case $From_Date_Buffer < _NowDateDayMonthYear() MsgBox (0, "Date Selection Error 101", $From_Date_Buffer & " < " & _NowDateDayMonthYear()) Case $From_Date_Buffer > $To_Date_Buffer MsgBox (0, "Date Selection Error 102", $From_Date_Buffer & " > " & $To_Date_Buffer) EndSelect EndSwitch WEndAny and all help much appreciated.-icu Edited June 26, 2011 by icu Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted June 22, 2011 Moderators Share Posted June 22, 2011 icu, I think this does what you want: expandcollapse popup#include <GUIConstantsEx.au3> #include <Date.au3> #include <DateTimeConstants.au3> #include <Array.au3> Global $aMonths[12] = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] $Form_Date_Test = GUICreate("Form2", 413, 96, 233, 476) $Label_From_Date = GUICtrlCreateLabel("From Date:", 8, 24, 59, 17) $From_Date = GUICtrlCreateDate(_NowCalc(), 72, 16, 121, 25) $Label_To_Date = GUICtrlCreateLabel("To Date:", 224, 24, 46, 17) $To_Date = GUICtrlCreateDate(_DateAdd('M', 1, _NowCalc()), 280, 16, 121, 25) GUISetState(@SW_SHOW) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $From_Date ; Get dates in correct format $sFromDate = _GetDate($From_Date) $sToDate = _GetDate($To_Date) ; Do the comparisons If _DateDiff("D", $sFromDate, _NowCalc()) > 0 Then ConsoleWrite("FROM earlier than TODAY" & @CRLF) EndIf If _DateDiff("D", $sToDate, $sFromDate) > 0 Then ConsoleWrite("FROM later than TO" & @CRLF) EndIf Case $To_Date ; Get dates in correct format $sFromDate = _GetDate($From_Date) $sToDate = _GetDate($To_Date) ; Do the comparisons If _DateDiff("D", $sToDate, _NowCalc()) > 0 Then ConsoleWrite("To earlier than TODAY" & @CRLF) EndIf If _DateDiff("D", $sToDate, $sFromDate) > 0 Then ConsoleWrite("FROM later than TO" & @CRLF) EndIf EndSwitch WEnd Func _GetDate($hCID) ; Split date parts $aDate = StringSplit(GUICtrlRead($hCID), " ") ; Convert month name to number $iMon = StringFormat("%02i", _ArraySearch($aMonths, $aDate[2]) + 1) ; Return the correct format Return $aDate[3] & "/" & $iMon & "/" & $aDate[1] EndFunc The trick is getting the date from the input into the correct format. I cannot find an easier way to get the month number from the name - perhaps someone else can help there. All clear? Please ask if not. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
icu Posted June 22, 2011 Author Share Posted June 22, 2011 @ Melba23: Result! Thank you so much for your help! If you could be so kind to talk me through the thinking behind your solution so I can gain more insight into AutoIt? When I look at your code I think understand why you put _GetDate() within the case... is it to make sure that the string in $sFromDate/$sToDate is 'comparable' to other dates? Frankly the line If _DateDiff("D", $sFromDate, _NowCalc()) > 0 Then is brilliant. I didn't even know about _DateDiff. I see that you calculate the day difference and if it is greater than zero you fire off the console warning. How did you know that StringSplit(GUICtrlRead($hCID), " ") was the correct solution for getting the dates out of the calendar handle? Also can you talk me though the "%02i" and the array bits of the following: ; Convert month name to number $iMon = StringFormat("%02i", _ArraySearch($aMonths, $aDate[2]) + 1) ; Return the correct format Return $aDate[3] & "/" & $iMon & "/" & $aDate[1] Many thanks for your help! Kind regards, icu Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted June 22, 2011 Moderators Share Posted June 22, 2011 icu, If you could be so kind to talk me through the thinking behind your solution so I can gain more insight into AutoIt?Of course! I think understand why you put _GetDate() within the case... is it to make sure that the string in $sFromDate/$sToDate is 'comparable' to other dates?Exactly. The Date inputs return "DD Mon YYYY" and we need "YYYY/MM/DD" to put into _DateDiff. I didn't even know about _DateDiffGreat thing that Help file! you calculate the day difference and if it is greater than zero you fire off the console warning.Correct again - are you sure you need me to explain things? How did you know that StringSplit(GUICtrlRead($hCID), " ") was the correct solution for getting the dates out of the calendarNatural genius! Seriously, StringSplit will break strings into arrays - so it seemed the perfect solution to get the 3 parts of the date from the content of the Date input. can you talk me though the "%02i" and the array bitsThe array bits are easy - they are the elements of the array produced by StringSplit. As the Date input gives us "DD Mon YYYY" and we split on the spaces, we get the following array: [0] 3 (this is the number of elements found) [1] DD [2] Mon [3] YYYY We need to get "YYYY/MM/DD" for _DateDiff. We already have YYYY and DD - we need to transform "Mon" (the name of a month) into "MM" (the number of the month). AutoIt has _DateToMonth which does it the other way round, but we need to write our own code: $iMon = StringFormat("%02i", _ArraySearch($aMonths, $aDate[2]) + 1) Breaking this down we get: _ArraySearch($aMonths, $aDate[2]) - this returns the index of the name in the array we created at the top of the script + 1 - we need this because the array starts at [0] and "January" should return "1" StringFormat("%02i" - this pads the numbers to exactly 2 integers by adding leading zeroes - so 6 becomes 06 while 11 stays as 11 Now we have the "MM" value we need and we can simply concatenate (join together) the various strings to get the required format for _DateDiff: $aDate[3] & "/" & $iMon & "/" & $aDate[1] All clear? Please ask again if not. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
icu Posted June 22, 2011 Author Share Posted June 22, 2011 @ Melba23: Perfect, natural genius for sure! ;-) Thanks once again for such a clear explanation! - icu Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted June 22, 2011 Moderators Share Posted June 22, 2011 icu, Glad I could help. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
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