Sign in to follow this  
Followers 0
3mustgetbeers

Buttons 'disabled' during countdown

8 posts in this topic

Hi Guys,

I'm writing a script that will backup my dads palm. It will give a gui stating '15 seconds till palm desktop is closed. click ok to run now or cancel to abort'

I have it more or less all (backup the database, compress, encrypt etc) but the buttons (ok, cancel & red X) won't work when the loop is in for the 15 second countdown - if i comment it out they work! Its probably pretty simple but I've been scratching my head for a couple of days and just don't get it. Any input is much appreciated - no matter how little if it sheds some light then thats great!

Thanks

Andy

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Palm Backup", 498, 271, 192, 124)
$Warning = GUICtrlCreateLabel("Warning", 200, 16, 89, 28)
        GUICtrlSetFont(-1, 16, 800, 0, "ariel")
$Text1 = GUICtrlCreateLabel("Palm is about to backup closing ALL open appointments", 48, 64, 394, 22)
        GUICtrlSetFont(-1, 12, 400, 0, "ariel")
$Text2 = GUICtrlCreateLabel("To cancel this backup in order to save changes click 'Cancel' within", 16, 96, 465, 22)
        GUICtrlSetFont(-1, 12, 400, 0, "ariel")
$CntDwn = GUICtrlCreateLabel("15", 232, 136, 26, 26)
        GUICtrlSetFont(-1, 14, 800, 0, "ariel")
$Text3 = GUICtrlCreateLabel("seconds", 216, 176, 63, 22)
        GUICtrlSetFont(-1, 12, 400, 0, "ariel")
$OK = GUICtrlCreateButton("Ok", 160, 224, 75, 25, $WS_GROUP)
$Cancel = GUICtrlCreateButton("Cancel", 256, 224, 75, 25, $WS_GROUP)
        GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1

        $nMsg = GUIGetMsg()
        Switch $nMsg
        Case $GUI_EVENT_CLOSE
                        Exit

                Case $OK
                        GUIDelete($Form1)
                        backup()
                Case $Cancel
                        GUIDelete($Form1)
                        Exit
        EndSwitch

    For $i =15 to 1 Step -1
           GUICtrlSetData($CntDwn, $i)
                Sleep(1000)
    Next
                            
        GUICtrlSetData($CntDwn, "0")
        Sleep(1000)

        ExitLoop
WEnd

Share this post


Link to post
Share on other sites



It's simple. You need just to get messages, again, in there loop with GuiGetMsg() and if result is $Cancel or $GUI_EVENT_CLOSE do stuff... or you can use event mode [Check in help file GUISetOnEvent()].


When the words fail... music speaks

Share this post


Link to post
Share on other sites

Andreik, thanks for that. I've had a look at the help file have come up with this:

For $i =15 to 1 Step -1
               GUICtrlSetData($CntDwn, $i)
                Sleep(1000)
            Next
        If GUIGetMsg($Cancel) Then
            Exit
        ElseIf GUIGetMsg($OK) Then
            Backup()
        ElseIf GuiGetMsg($GUI_EVENT_CLOSE) Then
            Exit
            EndIf
        GUICtrlSetData($CntDwn, "0")
        Sleep(1000)
        ExitLoop

This still doesn't work, but I don't get a syntax error either? I have mode the ifelse statements about in the for loop but to no effect?

Cheers

Andy

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Wrong syntax. Use in loop this style of switch.

Switch GuiGetMsg()
   Case $Cancel
      [stuff]
   Case $GUI_EVENT_CLOSE
      [stuff]
   Case $Ok
      [stuff]
EndSwitch
Edited by Andreik

When the words fail... music speaks

Share this post


Link to post
Share on other sites

Andreik, cheers for your help mate, I now have

While 1

        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit

            Case $OK
                GUIDelete($Form1)
                backup()
            Case $Cancel
                GUIDelete($Form1)
                Exit

            Case $CntDwn
                For $i =100 to 1 Step -1
                    GUICtrlSetData($CntDwn, $i)
                    Sleep(1000)
                Next
                    GUICtrlSetData($CntDwn, "0")
                    Sleep(1000)
                ExitLoop

        EndSwitch


WEnd

However the buttons work but now the countdown doesn't! :mellow:

Share this post


Link to post
Share on other sites

3mustgetbeers,

You need 2 loops to make it work - look at this:

While 1
    ; Start the loop for the countdown
    For $i = 15 To 1 Step -1
        ; Write countdown label
        GUICtrlSetData($CntDwn, $i)
        ; Get a timestamp
        $iBegin = TimerInit()
        ; Now run an idle loop for 1 sec checking for the buttons
        Do
            ; Check if a button is pressed
            Switch GUIGetMsg()
                Case $GUI_EVENT_CLOSE, $cancel ; they do the same thing, so put them on the same line!
                    Exit
                Case $OK
                    GUIDelete($Form1)
                    ;backup()
                    ConsoleWrite("backup called" & @CRLF)
                    Exit
            EndSwitch
        ; Check if we should exit the 1 sec idle loop
        Until TimerDiff($iBegin) > 1000
        ; A second has passed so loop the countdown loop
    Next
    ; We are now out of the countdown loop
    GUICtrlSetData($CntDwn, "0")
    Sleep(1000)

    ExitLoop
WEnd

All clear? :mellow:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

Melba23 -> works perfect mate, wouldn't expect anything less from your good self! Thanks alot!

Andreik thanks for pushing me in the right direction mate, you should enter me into the Guiness World Records - longest time taken for a penny to drop!

Thanks again to both of you!

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Easier (IMO) if you don't use those loops at all.

[autoit]

#include <ButtonConstants.au3>

#include <GUIConstantsEx.au3>

#include <StaticConstants.au3>

#include <WindowsConstants.au3>

#include <timers.au3>

#Region ### START Koda GUI section ### Form=

$Form1 = GUICreate("Palm Backup", 498, 271, 192, 124)

$Warning = GUICtrlCreateLabel("Warning", 200, 16, 89, 28)

GUICtrlSetFont(-1, 16, 800, 0, "ariel")

$Text1 = GUICtrlCreateLabel("Palm is about to backup closing ALL open appointments", 48, 64, 394, 22)

GUICtrlSetFont(-1, 12, 400, 0, "ariel")

$Text2 = GUICtrlCreateLabel("To cancel this backup in order to save changes click 'Cancel' within", 16, 96, 465, 22)

GUICtrlSetFont(-1, 12, 400, 0, "ariel")

$CntDwn = GUICtrlCreateLabel("15", 232, 136, 26, 26)

GUICtrlSetFont(-1, 14, 800, 0, "ariel")

$Text3 = GUICtrlCreateLabel("seconds", 216, 176, 63, 22)

GUICtrlSetFont(-1, 12, 400, 0, "ariel")

$OK = GUICtrlCreateButton("Ok", 160, 224, 75, 25, $WS_GROUP)

$Cancel = GUICtrlCreateButton("Cancel", 256, 224, 75, 25, $WS_GROUP)

GUISetState(@SW_SHOW)

#EndRegion ### END Koda GUI section ###

Global $MaxTime = 15;sec

_Timer_SetTimer ($Form1, 1000, "Count")

While 1

$nMsg = GUIGetMsg()

Select

case $nMsg = $GUI_EVENT_CLOSE

Exit

Case $nMsg = $OK Or $MaxTime = -1

GUIDelete($Form1)

backup ()

exit

Case $nMsg = $Cancel

GUIDelete($Form1)

Exit

EndSelect

WEnd

Func backup()

msgbox(0,"backing up","ok")

EndFunc

Func Count($a, $b, $c, $d)

$MaxTime -= 1

GUICtrlSetData($CntDwn, $MaxTime )

EndFunc ;==>DoWhatever

[\autoit]

Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

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
Sign in to follow this  
Followers 0