Jump to content

Problems with GUICtrlCreateDate [solved]


icu
 Share

Recommended Posts

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_Date

However 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:

#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
WEnd

Any and all help much appreciated.

-icu

Edited by icu
Link to comment
Share on other sites

  • Moderators

icu,

I think this does what you want: :huh2:

#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. :alien:

All clear? Please ask if not. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

@ 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

  • Moderators

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! :ph34r:

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 _DateDiff

Great thing that Help file! :alien:

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? :huh2:

How did you know that StringSplit(GUICtrlRead($hCID), " ") was the correct solution for getting the dates out of the calendar

Natural 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 bits

The 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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

icu,

Glad I could help. :huh2:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...