Sign in to follow this  
Followers 0
wakillon

Yet another GuiOnEventMode problem

4 posts in this topic

I want replace my download button by a cancel button

But when i click on cancel button nothing happens ...Posted Image

I'm lost, since 3 days I tearing my hair !

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
Opt ( "GuiOnEventMode", 1 ) 
Global $_GUI,$_DownloadButton, $_Cancel = FALSE, $_CancelButton

_Gui ( )

While 1
   Sleep ( 10 )
WEnd

Func _Gui ( )
    $_GUI = GUICreate ( "GUI", 400, 200 )
    GUISetOnEvent ( $GUI_EVENT_CLOSE, "_Exit" )
    
    $_DownloadButton = GUICtrlCreateButton ( "Download", 10, 90, 180, 24 )
    GUICtrlSetOnEvent ( $_DownloadButton, "_Download" ) 
    GUICtrlSetState ( $_DownloadButton, $GUI_ENABLE ) 
    
    $_CancelButton = GUICtrlCreateButton ( "Cancel Download", 10, 90, 180, 24 )
    GUICtrlSetOnEvent ( $_CancelButton, "_CancelDownload" )
    GUICtrlSetState ( $_CancelButton, $GUI_DISABLE ) 
    GUICtrlSetState ( $_CancelButton, $GUI_HIDE ) 
    
    GUISetState ( @SW_SHOW )
EndFunc ;==> _Gui ( )

Func _CancelDownload ( )
    ConsoleWrite ( "+>---- Function _CancelDownload ( ) : " & @Crlf )   
    $_Cancel=TRUE
    ConsoleWrite ( "+>---- $_Cancel : " & $_Cancel & @Crlf )    
EndFunc 

Func _Download ( )
    ConsoleWrite ( "+>---- Function _Download ( ) : " & @Crlf ) 
    GUICtrlSetState ( $_DownloadButton, $GUI_DISABLE )  
    GUICtrlSetState ( $_DownloadButton, $GUI_HIDE ) 
    GUICtrlSetState ( $_CancelButton, $GUI_SHOW )   
    GUICtrlSetState ( $_CancelButton, $GUI_ENABLE )
    
    While 1
        If $_Cancel = TRUE Then
            $_MsgBox = MsgBox ( 4, "Cancel", "Are you sure to Cancel Download ?" )
            If $_MsgBox = 6 Then 
                $_Cancel = FALSE
                ExitLoop
            EndIf   
        EndIf   
        Sleep ( 10 )
    WEND
    
    GUICtrlSetState ( $_CancelButton, $GUI_DISABLE ) 
    GUICtrlSetState ( $_CancelButton, $GUI_HIDE ) 
    GUICtrlSetState ( $_DownloadButton, $GUI_SHOW )     
    GUICtrlSetState ( $_DownloadButton, $GUI_ENABLE )   
EndFunc ;==> _Download ( )

Func _Exit ( )
    GUIDelete ( $_GUI )
    Exit
EndFunc ;==> _Exit ( )

I saw during my research that Melba23 was a great specialist of this kind of problems ! Posted Image


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

The problem is that the _CancelDownload ( ) function has to wait for the _Download function to finish before it's called and by then it's too late. I'll cook up an example to work around it.

edit:

Blast! too late!

here was my suggestion:

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
Opt ( "GuiOnEventMode", 1 )
Global $_GUI,$_ToggleButton, $_Cancel = True

_Gui ( )
While 1
    If Not $_Cancel Then
        _Download ( )
    EndIf
 Sleep ( 10 )
WEnd

Func _Gui ( )
    $_GUI = GUICreate ( "GUI", 400, 200 )
    GUISetOnEvent ( $GUI_EVENT_CLOSE, "_Exit" )
    $_ToggleButton = GUICtrlCreateButton ( "Download", 10, 90, 180, 24 )
    GUICtrlSetOnEvent ( $_ToggleButton, "_ToggleDownload" )
    GUISetState ( @SW_SHOW )
EndFunc ;==> _Gui ( )

Func _ToggleDownload ( )
    ConsoleWrite ( "+>---- Function _ToggleDownload ( ) : " & @Crlf )
    $_Cancel = Not $_Cancel
    If $_Cancel Then
        GUICtrlSetData($_ToggleButton,"Download")
    Else
        GUICtrlSetData($_ToggleButton,"Cancel")
    EndIf
    ConsoleWrite ( "+>---- $_Cancel : " & $_Cancel & @Crlf )
EndFunc

Func _Download ( )
    ConsoleWrite ( "+>---- Function _Download ( ) : " & @Crlf )
    While 1
    If $_Cancel = TRUE Then
    $_MsgBox = MsgBox ( 4, "Cancel", "Are you sure to Cancel Download ?" )
    If $_MsgBox = 6 Then
    ExitLoop
    Else
                $_Cancel = False
            EndIf
    EndIf
    Sleep ( 10 )
    WEnd
EndFunc ;==> _Download ( )

Func _Exit ( )
    GUIDelete ( $_GUI )
    Exit
EndFunc ;==> _Exit ( )

Pretty much the same idea.

Edited by Tvern

Share this post


Link to post
Share on other sites

wakillon,

I would do something along these lines: ;)

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
Opt ( "GuiOnEventMode", 1 )
Global $_GUI,$_DualButton, $_Cancel = FALSE

_Gui ( )

While 1

    If $_Cancel = TRUE Then
        $_MsgBox = MsgBox ( 4, "Cancel", "Are you sure to Cancel Download ?" )
        If $_MsgBox = 6 Then
            $_Cancel = FALSE
            ExitLoop
        EndIf
    EndIf
    Sleep ( 10 )

WEnd

Func _Gui ( )
    $_GUI = GUICreate ( "GUI", 400, 200 )
    GUISetOnEvent ( $GUI_EVENT_CLOSE, "_Exit" )

    $_DualButton = GUICtrlCreateButton ( "Download", 10, 90, 180, 24 )
    GUICtrlSetOnEvent ( $_DualButton, "_Button" )

    GUISetState()
EndFunc

Func _Button ( )
    If GUICtrlRead($_DualButton) = "Download" Then
        ConsoleWrite ( "+>---- Function _Download ( ) : " & @Crlf )
        GUICtrlSetData($_DualButton, "Cancel Download")
    Else
        ConsoleWrite ( "+>---- Function _CancelDownload ( ) : " & @Crlf )
        $_Cancel=TRUE
        ConsoleWrite ( "+>---- $_Cancel : " & $_Cancel & @Crlf )
        GUICtrlSetData($_DualButton, "Download")
    EndIf
EndFunc ;==> _Download ( )

Func _Exit ( )
    Exit
EndFunc ;==> _Exit ( )

Have you read the Interrupting a running function tutorial in the Wiki? Lots of good info on how to do it in there. :)

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

Wow 12 minutes and 2 working solutions Posted Image

Melba23 and Tvern thank you very much !

You're great and fast !

what i'm stupid by moment...

Thanks again ! Posted Image


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

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