Jump to content

Button on timer


Recommended Posts

Hi, I need help with somewhat of a amateur question related to a timer and a button. Essentially what I'm trying to make is a timer that will countdown and there is a button that will let the user close the timer, but as the timer is counting down the button doesn't work even when the timer has stopped the button still does not work. Here is the code

#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <EditConstants.au3>

GUIFUNC()

Opt("GUIOnEventMode", 1)

Func GUIFUNC()
    $ms = 10000
    $timer = TimerInit()

    GUICreate("", 350, 330)

    $Input = GUICtrlCreateInput("", 125, 150, 90, 40, BitOR($SS_CENTER, $ES_READONLY, $WS_CLIPCHILDREN))
    GUICtrlSetFont(-1, 18, 400)

    Global $Button1 = GUICtrlCreateButton("Close", 125, 250, 90, 40)
    GUICtrlSetOnEvent(-1, "PressClose")

    GUISetState(@SW_SHOW)


    While (TimerDiff($timer) < $ms)
        $seconds = TimerDiff($timer) / 1000
        $diff = $seconds - ($ms / 1000);
        $minutes = Int($diff / 60)
        $secondsRem = $diff - ($minutes * 60)
        $minutes = $minutes * -1
        $secondsRem = $secondsRem * -1
        $time = StringFormat("%02d", $minutes) & ":" & StringFormat("%02d", $secondsRem)
        GUICtrlSetData($Input, $time)
    WEnd

    While 1
        Sleep(10)
    WEnd
EndFunc

Func PressClose()

    Switch @GUI_CtrlId
        Case $Button1
            $Var = MsgBox(4, "Close", "Close?")
            If $Var = 6 Then Exit
    EndSwitch

EndFunc

Now I know this will work if I take off the Function commands , but I found this timer on these forums and edited to my liking and then tried to implement into a bigger program and the button did not work. This is basically how it would look like in the original program (original program uses OnEventMode). Any help is appreciated!

Link to comment
Share on other sites

;http://www.autoitscript.com/forum/topic/153564-button-on-timer/
;Post #1
;D:\DOKUME~1\ADMINI~1\LOKALE~1\Temp\SLICER\Avatar\default_large.png
;by Inknown

;Script grabbed by SLICER by Edano here: http://www.autoitscript.com/forum/topic/152402-slicer-autoit-forum-script-grabber/?p=1093575

#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <EditConstants.au3>

Opt("GUIOnEventMode", 1)

GUIFUNC()

Func GUIFUNC()
    $ms = 10000
    $timer = TimerInit()

    GUICreate("", 350, 330)

    $Input = GUICtrlCreateInput("", 125, 150, 90, 40, BitOR($SS_CENTER, $ES_READONLY, $WS_CLIPCHILDREN))
    GUICtrlSetFont(-1, 18, 400)

    Global $Button1 = GUICtrlCreateButton("Close", 125, 250, 90, 40)
    GUICtrlSetOnEvent(-1, "PressClose")

    GUISetState(@SW_SHOW)


    While (TimerDiff($timer) < $ms)
        $seconds = TimerDiff($timer) / 1000
        $diff = $seconds - ($ms / 1000);
        $minutes = Int($diff / 60)
        $secondsRem = $diff - ($minutes * 60)
        $minutes = $minutes * -1
        $secondsRem = $secondsRem * -1
        $time = StringFormat("%02d", $minutes) & ":" & StringFormat("%02d", $secondsRem)
        GUICtrlSetData($Input, $time)
    WEnd

    While 1
        Sleep(10)
    WEnd
EndFunc

Func PressClose()

    Switch @GUI_CtrlId
        Case $Button1
            $Var = MsgBox(4, "Close", "Close?")
            If $Var = 6 Then Exit
    EndSwitch

EndFunc

:)

[color=rgb(255,0,0);][font="'comic sans ms', cursive;"]FukuLeaks[/color][/font]

Link to comment
Share on other sites

WOW lol took me a while to see what you changed! Did not know that would make a difference thank you!

.

the opt() wasn't executed because you put it after the main function.

it took me also quite a while to find the problem, because you did not make the most usual errors.... :)

[color=rgb(255,0,0);][font="'comic sans ms', cursive;"]FukuLeaks[/color][/font]

Link to comment
Share on other sites

Well now I have another dilemma I tried to implement it with another GUI, but I still get the same problem:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>
#include <EditConstants.au3>
#include <GUIConstants.au3>

Opt("GUIOnEventMode", 1)

GUIFUNC()

Func GUIFUNC()
    Global $Form1 = GUICreate("Countdown", 460, 238, 288, 136)

    Global $Button1 = GUICtrlCreateButton("Countdown", 144, 80, 177, 57)
    GUICtrlSetFont($Button1, 12, 800, 0, "MS Serif")
    GUICtrlSetOnEvent(-1, "PressButton")

    GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents")

    GUISetState(@SW_SHOW)

    While 1
        Sleep(10)
    WEnd
EndFunc   ;==>GUIFUNC

Func GUIFUNC2()
    $ms = 10000
    $timer = TimerInit()

    GUICreate("", 350, 330)

    $Input = GUICtrlCreateInput("", 125, 150, 90, 40, BitOR($SS_CENTER, $ES_READONLY, $WS_CLIPCHILDREN))
    GUICtrlSetFont(-1, 18, 400)

    Global $Button1 = GUICtrlCreateButton("Close", 125, 250, 90, 40)
    GUICtrlSetOnEvent(-1, "PressClose")

    GUISetState(@SW_SHOW)


    While (TimerDiff($timer) < $ms)
        $seconds = TimerDiff($timer) / 1000
        $diff = $seconds - ($ms / 1000);
        $minutes = Int($diff / 60)
        $secondsRem = $diff - ($minutes * 60)
        $minutes = $minutes * -1
        $secondsRem = $secondsRem * -1
        $time = StringFormat("%02d", $minutes) & ":" & StringFormat("%02d", $secondsRem)
        GUICtrlSetData($Input, $time)
    WEnd

    While 1
        Sleep(10)
    WEnd

EndFunc   ;==>GUIFUNC2

Func PressClose()

    Switch @GUI_CtrlId
        Case $Button1
            $Var = MsgBox(4, "Close", "Close?")
            If $Var = 6 Then Exit
    EndSwitch

EndFunc   ;==>PressClose

Func SpecialEvents()
    Switch @GUI_CtrlHandle
        Case $Form1
            Exit
    EndSwitch
EndFunc   ;==>SpecialEvents

Func PressButton()
    Switch @GUI_CtrlId
        Case $Button1
            GUIFUNC2()
    EndSwitch
EndFunc   ;==>PressButton
Link to comment
Share on other sites

You have to leave a function before you try to do what you're doing. Try this.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>
#include <EditConstants.au3>
#include <GUIConstants.au3>
Opt("GUIOnEventMode", 1)
Global $Input
GUIFUNC()
While 1
    Sleep(10)
WEnd

Func GUIFUNC()
    GUICreate("Countdown", 460, 238, 288, 136)
    GUICtrlCreateButton("Countdown", 144, 80, 177, 57)
    GUICtrlSetFont(-1, 12, 800, 0, "MS Serif")
    GUICtrlSetOnEvent(-1, "PressButton")
    GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents")
    GUISetState()
EndFunc   ;==>GUIFUNC
Func GUIFUNC2()
    AdlibRegister("_Timer")
    GUICreate("", 350, 330)
    $Input = GUICtrlCreateInput("", 125, 150, 90, 40, BitOR($SS_CENTER, $ES_READONLY, $WS_CLIPCHILDREN))
    GUICtrlSetFont(-1, 18, 400)
    $Button2 = GUICtrlCreateButton("Close", 125, 250, 90, 40)
    GUICtrlSetOnEvent($Button2, "PressClose")
    GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents")
    GUISetState()
EndFunc   ;==>GUIFUNC2
Func PressClose()
    $Var = MsgBox(4, "Close", "Close?")
    If $Var = 6 Then Exit
EndFunc   ;==>PressClose
Func SpecialEvents()
    Exit
EndFunc   ;==>SpecialEvents
Func PressButton()
    GUIFUNC2()
EndFunc   ;==>PressButton
Func _Timer()
    Local Static $ms = 10000
    Local Static $timer = TimerInit()
    Local $seconds = TimerDiff($timer) / 1000
    Local $diff = $seconds - ($ms / 1000);
    Local $minutes = (Int($diff / 60)) * -1
    Local $secondsRem = ($diff - ($minutes * 60)) * -1
    GUICtrlSetData($Input, StringFormat("%02d", $minutes) & ":" & StringFormat("%02d", $secondsRem))
EndFunc   ;==>_Timer

I got rid of 90% of the stuff in your script that was overkill or pointless to what you needed it to do. As you can see by my script, there's one While loop to keep the GUIs alive, and I always return from each function prior to trying to execute the next one. I turned your timer into an AdLib function, because otherwise it's going to hang your script until it's done.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

InKnown,

Similar to BrewmanNH's (beat me to it again!!!) except that I consolidated your events to eliminate redundant code.

M23 has an excellent tutorial here for managing multiple Gui's.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>
#include <EditConstants.au3>
#include <GUIConstants.au3>

Opt("GUIOnEventMode", 1)

global $form2, $button2, $input, $timer

GUIFUNC()

Func GUIFUNC()
    Global $Form1 = GUICreate("Countdown", 460, 238, 288, 136)
    GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents")

    Global $Button1 = GUICtrlCreateButton("Countdown", 144, 80, 177, 57)
    GUICtrlSetFont($Button1, 12, 800, 0, "MS Serif")
    GUICtrlSetOnEvent(-1, "Button")

    GUISetState(@SW_SHOW)

    While 1
        Sleep(10)
    WEnd
EndFunc   ;==>GUIFUNC

Func GUIFUNC2()

    $form2 = GUICreate("", 350, 330)
    GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents")

    $Input = GUICtrlCreateInput("", 125, 150, 90, 40, BitOR($SS_CENTER, $ES_READONLY, $WS_CLIPCHILDREN))
    GUICtrlSetFont(-1, 18, 400)

    $Button2 = GUICtrlCreateButton("Close", 125, 250, 90, 40)
    GUICtrlSetOnEvent(-1, "Button")

    GUISetState(@SW_SHOW)

EndFunc   ;==>GUIFUNC2

Func Button()

    switch @gui_ctrlid
        case $button2
            $Var = MsgBox(4, "Close", "Close?")
            If $Var = 6 Then Exit
        case $Button1
            guifunc2()
            $timer = timerinit()
            Update_timer()
            adlibregister('Update_timer',1000)
    endswitch

EndFunc   ;==>PressClose

Func SpecialEvents()

    Switch @GUI_winhandle
        Case $form1
            Exit
        case $form2
            guidelete($form2)
    EndSwitch

EndFunc   ;==>SpecialEvents

func Update_timer()

        $ms = 10000

        $seconds = TimerDiff($timer) / 1000
        $diff = $seconds - ($ms / 1000);
        $minutes = Int($diff / 60)
        $secondsRem = $diff - ($minutes * 60)
        $minutes = $minutes * -1
        $secondsRem = $secondsRem * -1
        $time = StringFormat("%02d", $minutes) & ":" & StringFormat("%02d", $secondsRem)
        GUICtrlSetData($Input, $time)

endfunc

kylomas

edit: moved timerinit function from adlib to button routine so it could be retstarted from the beginning

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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...