Jump to content

The problem with the OnEvent mode


AndreyS
 Share

Recommended Posts

All greetings! Please tell me how to change the program code that, when

Pressing the button "Exit" the program was closed after a pre-pressed "Ok"?

Thank you!

Sorry for my English! :)

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1) ; Change to OnEvent mode

$mainwindow = GUICreate("Hello World", 200, 150)

GUICtrlCreateLabel("Hello world! How are you?", 30, 10)

$okbutton = GUICtrlCreateButton("OK", 70, 50, 60)

GUICtrlSetOnEvent($okbutton, "OKButton")

$exitbutton = GUICtrlCreateButton("Exit", 70, 80, 60)

GUICtrlSetOnEvent($exitbutton , "CLOSEClicked")

GUISetState(@SW_SHOW)

While 1

Sleep(1000) ; Idle around

WEnd

Func OKButton()

MsgBox(0, "GUI Event", "You pressed OK!")

While 1

Sleep(1000)

WEnd

EndFunc

Func CLOSEClicked()

MsgBox(0, "GUI Event", "You clicked CLOSE! Exiting...")

Exit

EndFunc

Link to comment
Share on other sites

Please use the code tags when posting code. It makes it much easier to read.

On the toolbar click the button that looks like "<>" and paste your code between the tags.

#Include <GUIConstantsEx.au3>
Opt("GUIOnEventMode", 1) ; Change to OnEvent mode
Global $bExit = False
$mainwindow = GUICreate("Hello World", 200, 150)
GUICtrlCreateLabel("Hello world! How are you?", 30, 10)
$okbutton = GUICtrlCreateButton("OK", 70, 50, 60)
GUICtrlSetOnEvent($okbutton, "OKButton")
$exitbutton = GUICtrlCreateButton("Exit", 70, 80, 60)
GUICtrlSetState($exitbutton, $GUI_DISABLE)
GUICtrlSetOnEvent($exitbutton, "CLOSEClicked")

GUISetState(@SW_SHOW)

While 1
    Sleep(1000) ; Idle around
WEnd

Func OKButton()
    MsgBox(0, "GUI Event", "You pressed OK!")
    $bExit = NOT $bExit
    If $bExit Then
        GUICtrlSetState($exitbutton, $GUI_ENABLE)
    EndIf
    ;While 1
    ;   Sleep(1000)
    ;WEnd
EndFunc   ;==>OKButton

Func CLOSEClicked()
    MsgBox(0, "GUI Event", "You clicked CLOSE! Exiting...")
    Exit
EndFunc   ;==>CLOSEClicked

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

That's the problem that you have transferred to comment cycle that I absolutely need it! I've got a big chunk of code that I removed specifically for ease!

The problem is this: how to get out of the cycle which started in the function?

Edited by AndreyS
Link to comment
Share on other sites

I commented that because it was useless and with a sleep your code would stay right there. If you are actually doing something in that loop then you must have a condition that will allow you to call ExitLoop or Return which are in the Help file under Keyword/Statement Reference.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

So I need only when you click on the button "Exit" came out of the loop! But how?

If the cycle is not in a function body then when you click "Exit" cycle is interrupted. But unless he is in a function that does not stop it by pressing the button

Edited by AndreyS
Link to comment
Share on other sites

Thats one of the reasons I hate using that archaic onevent code. Try this and if it's doing what you want then we can work out some other problems you may run into.

#Include <GUIConstantsEx.au3>
;Opt("GUIOnEventMode", 1) ; Change to OnEvent mode
Global $bExit = False
$mainwindow = GUICreate("Hello World", 200, 150)
GUICtrlCreateLabel("Hello world! How are you?", 30, 10)
$okbutton = GUICtrlCreateButton("OK", 70, 50, 60)
;GUICtrlSetOnEvent($okbutton, "OKButton")
$exitbutton = GUICtrlCreateButton("Exit", 70, 80, 60)
GUICtrlSetState($exitbutton, $GUI_DISABLE)
;GUICtrlSetOnEvent($exitbutton, "CLOSEClicked")

GUISetState(@SW_SHOW)

While 1
    ;Sleep(1000) ; Idle around
    If GUIGetMsg() = $okbutton Then OKButton()
WEnd

Func OKButton()
    MsgBox(0, "GUI Event", "You pressed OK!")
    $bExit = NOT $bExit
    If $bExit Then
        GUICtrlSetState($exitbutton, $GUI_ENABLE)
    EndIf
    While 1
       If GUIGetMsg() = $exitbutton Then CLOSEClicked()
    WEnd
EndFunc   ;==>OKButton

Func CLOSEClicked()
    MsgBox(0, "GUI Event", "You clicked CLOSE! Exiting...")
    Exit
EndFunc   ;==>CLOSEClicked

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

It is not strange, but the following method works!

;~ #include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)  ; Change to OnEvent mode 
Opt("TrayMenuMode", 1)
Opt("TrayOnEventMode", 1)

$mExit = TrayCreateItem("Выход")
TrayItemSetOnEvent(-1, "CLOSEClicked")

$mainwindow = GUICreate("Hello World", 200, 150)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
GUICtrlCreateLabel("Hello world! How are you?", 30, 10)
$okbutton = GUICtrlCreateButton("OK", 70, 50, 60)
GUICtrlSetOnEvent($okbutton, "OKButton")

$exitbutton = GUICtrlCreateButton("Exit", 70, 80, 60)
GUICtrlSetOnEvent($exitbutton , "CLOSEClicked")

GUISetState(@SW_SHOW)

While 1
  Sleep(1000)  ; Idle around
WEnd

Func OKButton()
   MsgBox(0, "GUI Event", "You pressed OK!")
   While 1
      Sleep(1000)
   WEnd
EndFunc

Func CLOSEClicked()
  MsgBox(0, "GUI Event", "You clicked CLOSE! Exiting...")
  Exit
EndFunc

Why not work pressing the button "Exit"?

And what distinguishes the function call TrayItemSetOnEvent(-1, "CLOSEClicked") of the function call GUICtrlSetOnEvent($exitbutton , "CLOSEClicked")?

Why is the first version of the call such a high priority?

Link to comment
Share on other sites

The 1970s called and they want their code back. I have one app with over a thousand controls on 30 forms and I have no problems using a message loop to handle them.

We really don't have enough of your code to give you a viable solution to your particular problem. I suggest that you take a look at the AdLib functions though.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Why are you stalling the script out in that function at all.

[revised] to show doing stuff in loop while button active

#Include <GUIConstantsEx.au3>
Opt("GUIOnEventMode", 1) ; Change to OnEvent mode
Global $bExit = False
$mainwindow = GUICreate("Hello World", 200, 150)
GUICtrlCreateLabel("Hello world! How are you?", 30, 10)
$okbutton = GUICtrlCreateButton("OK", 70, 50, 60)
GUICtrlSetOnEvent($okbutton, "OKButton")
$exitbutton = GUICtrlCreateButton("Exit", 70, 80, 60)
GUICtrlSetOnEvent($exitbutton, "CLOSEClicked")
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
GUICtrlSetState($exitbutton, $GUI_DISABLE)

GUISetState(@SW_SHOW)
$count=0
While 1
    Sleep(1000) ; Idle around
    if GUICtrlGetState($exitbutton)=80 Then
        $count=$count+1
        ToolTip("Doing stuff "&$count)
    EndIf
WEnd

Func OKButton()
 ;   MsgBox(0, "GUI Event", "You pressed OK!")
    $bExit = NOT $bExit
    If $bExit Then
        GUICtrlSetState($exitbutton, $GUI_ENABLE)
    EndIf

EndFunc   ;==>OKButton

Func CLOSEClicked()
   ; MsgBox(0, "GUI Event", "You clicked CLOSE! Exiting...")
    Exit
EndFunc   ;==>CLOSEClicked
Edited by picea892
Link to comment
Share on other sites

I understand that both will work, picea892, it's too easy!

That's the problem that when you click "OK" to start the function in which a continuous cycle! And when you click on the button "Exit" should occur out of this cycle / function! How?

Thank you for your responsiveness, picea892!

Link to comment
Share on other sites

Ok, let this decision '70s! But why is TrayItemSetOnEvent(-1, "CLOSEClicked") breaks the loop and this GUICtrlSetOnEvent($exitbutton , "CLOSEClicked") - no?

P.S. Sorry for the fact that time-consuming!

I don't know but I think maybe there is more than one type of event queue handling in AutoIt. Most events, like clicking a button say, are dealt with in turn so if one event calls a function no other event will be dealt with untill that function ends. My example code shows how you could get round this to some extent.

Tray Events must have a different queue so that a tray event can interrupt a gui event. That's my guess.

I tried it the other way round and gui events can interupt tray events. I didn't realize that before :)

#include <GUIConstantsEx.au3>


Opt("GUIOnEventMode", 1) ; Change to OnEvent mode
Opt("TrayMenuMode", 1)
Opt("TrayOnEventMode", 1)

$mExit = TrayCreateItem("?????")
TrayItemSetOnEvent(-1, "CLOSEClicked")
$testtray = TrayCreateItem("what happens?")
TrayItemSetOnEvent(-1, "oja")

$mainwindow = GUICreate("Hello World", 200, 150)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
GUICtrlCreateLabel("Hello world! How are you?", 30, 10)
$okbutton = GUICtrlCreateButton("OK", 70, 50, 60)
GUICtrlSetOnEvent($okbutton, "OKButton")

$exitbutton = GUICtrlCreateButton("Exit", 70, 80, 60)
GUICtrlSetOnEvent($exitbutton, "CLOSEClicked")

GUISetState(@SW_SHOW)
Global $oked = False

While 1
    Sleep(1000) ; Idle around
    If $oked Then OKButtonB()
WEnd
Func oja()

    MsgBox(2562144, "tray event", "ojo clicked")
    While 1
    Sleep(100)

    WEnd
EndFunc ;==>oja


Func OKButton()
    $oked = True
EndFunc ;==>OKButton

Func OKButtonB()
    MsgBox(0, "GUI Event", "You pressed OK!")
    While 1
    Sleep(1000)
    WEnd
EndFunc ;==>OKButtonB

Func CLOSEClicked()
    MsgBox(0, "GUI Event", "You clicked CLOSE! Exiting...")
    Exit
EndFunc ;==>CLOSEClicked
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.
Link to comment
Share on other sites

Interesting decision martin! :) Yes button works "Exit" after "Ok"!

But now if you click in the tray menu, "what happens?" it does not work anymore the button "Ok"! Why suddenly, I can not understand nothing? ;)

Um, you're right. It seems that if you set a tray event and a guictrl event to the same function then:

If you set the tray event after setting the guictrl event they both work.

If you set the tray event first and then set the guictrl event then the tray event is lost.

But this is a problem that has been fixed in the latest Beta release.

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.
Link to comment
Share on other sites

I wouldn't even use GUISETONEVENT. I just use a Switch or Select loop and use the GUIGETMSG() Func..

While 1
$msg = GUIGetMsg()
Switch $msg
Case $button1
     button1()
Cause $button2
     button2()
EndSwitch
Wend
Link to comment
Share on other sites

I wouldn't even use GUISETONEVENT. I just use a Switch or Select loop and use the GUIGETMSG() Func..

Yes, it is also a solution, but as I wrote above, it is not appropriate in my case, Gui! Thank you and you for your help!

If you set the tray event after setting the guictrl event they both work.

If you set the tray event first and then set the guictrl event then the tray event is lost.

I tried these two options! Neither version of the function GUICtrlSetOnEvent($exitbutton,"CLOSEClicked") does not work after press "Ok"! ;) Use version AutoIt 3.3.0.0

Ooo finally understand me! :) Thank you, martin!

And when some out the next stable version of AutoIt?

Link to comment
Share on other sites

And when some out the next stable version of AutoIt?

That is always an unknown so the only answer that even the Devs can give you is "It comes out when it comes out."

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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