Jump to content

guionevent with loops


JohnOne
 Share

Recommended Posts

Taken straight from the wiki managing multiple gui's but added a loop in _GUI2 function

It demonstrates a problem I'm stuck at, where I need an extra loop to keep my script

from wandering off beyond my control and failing.

Anyway, the following will not work.

#include <GUIConstantsEx.au3>
Opt("GUIOnEventMode", 1)
Global $hGUI2, $hButton2 ; Predeclare these variables
gui1()
Func gui1()
    $hGUI1 = GUICreate("Gui 1", 200, 200, 100, 100)
    GUISetOnEvent($GUI_EVENT_CLOSE, "On_Close_Main") ; Run this function when the main GUI [X] is clicked
    $hButton1 = GUICtrlCreateButton("Msgbox 1", 10, 10, 80, 30)
    GUICtrlSetOnEvent(-1, "On_Button1")
    $hButton2 = GUICtrlCreateButton("Show Gui 2", 10, 60, 80, 30)
    GUICtrlSetOnEvent(-1, "On_Button2")
    GUISetState()
    While 1
        Sleep(10)
    WEnd
EndFunc   ;==>gui1
Func gui2()
    $hGUI2 = GUICreate("Gui 2", 200, 200, 350, 350)
    GUISetOnEvent($GUI_EVENT_CLOSE, "On_Close_Secondary") ; Run this function when the secondary GUI [X] is clicked
    $hButton3 = GUICtrlCreateButton("MsgBox 2", 10, 10, 80, 30)
    GUICtrlSetOnEvent(-1, "On_Button3")
    GUISetState()

While 1
  Sleep(10) ; this is the situation in my script
                 ; if I have no loop the function will return
                 ; without its user input
WEnd
EndFunc   ;==>gui2
Func On_Close_Main()
    Exit
EndFunc
Func On_Close_Secondary()
    GUIDelete($hGUI2)
    GUICtrlSetState($hButton2, $GUI_ENABLE)
EndFunc
Func On_Button1()
    MsgBox("", "MsgBox 1", "Test from Gui 1")
EndFunc
Func On_Button2()
    GUICtrlSetState($hButton2, $GUI_DISABLE)
    gui2()
EndFunc
Func On_Button3()
    MsgBox("", "MsgBox 2", "Test from Gui 2")
EndFunc

I just cannot get my head around how to remove the loop

safely.

EDIT:

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

  • Moderators

JohnOne,

Just move the loop outside the function. Then the script always returns to the idle loop and you do not get stuck inside a function with all that entails for interrupting other functions if required. I might modify the Wiki example to reflect this. :D

So you get this: ;)

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

Global $hGUI2, $hButton2 ; Predeclare these variables

gui1()

While 1
    Sleep(10)
WEnd

Func gui1()
    $hGUI1 = GUICreate("Gui 1", 200, 200, 100, 100)
    GUISetOnEvent($GUI_EVENT_CLOSE, "On_Close_Main") ; Run this function when the main GUI [X] is clicked
    $hButton1 = GUICtrlCreateButton("Msgbox 1", 10, 10, 80, 30)
    GUICtrlSetOnEvent(-1, "On_Button1")
    $hButton2 = GUICtrlCreateButton("Show Gui 2", 10, 60, 80, 30)
    GUICtrlSetOnEvent(-1, "On_Button2")
    GUISetState()
EndFunc   ;==>gui1
Func gui2()
    $hGUI2 = GUICreate("Gui 2", 200, 200, 350, 350)
    GUISetOnEvent($GUI_EVENT_CLOSE, "On_Close_Secondary") ; Run this function when the secondary GUI [X] is clicked
    $hButton3 = GUICtrlCreateButton("MsgBox 2", 10, 10, 80, 30)
    GUICtrlSetOnEvent(-1, "On_Button3")
    GUISetState()
EndFunc   ;==>gui2
Func On_Close_Main()
    Exit
EndFunc
Func On_Close_Secondary()
    GUIDelete($hGUI2)
    GUICtrlSetState($hButton2, $GUI_ENABLE)
EndFunc
Func On_Button1()
    MsgBox("", "MsgBox 1", "Test from Gui 1")
EndFunc
Func On_Button2()
    GUICtrlSetState($hButton2, $GUI_DISABLE)
    gui2()
EndFunc
Func On_Button3()
    MsgBox("", "MsgBox 2", "Test from Gui 2")
EndFunc

Or have I misunderstood what you are trying to do? :)

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

Thanks M23, I've had a good think about that.

But yes, I think I need to clarify...

I basically need a blocking function, but Msgbox and InputBox are not fit for purpose.

When my script is running it is working away getting info from here and there, doing this

and that, running nice and sexy just the way I like it etc... When this particular function is

called though, I need to stop it in its tracks until it returns an array to its caller.

That's why I had a loop there, I first had a GuiGetMsg() loop until I remembered I was working

in event mode ( :) Its been some time since this script was started lol), thought I could

swap and change during runtime but that seemed to fail too.

I think my options are limited to recoding main gui to loop mode, which is a massive and boring task ;)

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

  • Moderators

JohnOne,

There is nothing to stop you using a loop in an OnEvent function. Once you are in an OnEvent function you are stuck there until you get back to the main idle loop as the Interrupting a running function tutorial in the Wiki shows. ;)

What is the criterion for exiting the loop? The completion of the array? :)

Could you post this blocking function in question so I could take a look? :D

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

This is its simplest form, only difference being the gui has an input control for a path and a little more info

Func _GetWndInfo()
Local $WndInfo[9]
$Form1 = GUICreate("Form1", 185, 119, 192, 124)
$Button1 = GUICtrlCreateButton("Button1", 48, 56, 75, 25)
$Label1 = GUICtrlCreateLabel("hover over the window and click", 16, 32, 158, 17)
GUISetState(@SW_SHOW)
While 1
  $nMsg = GUIGetMsg()
  Switch $nMsg
   Case $GUI_EVENT_CLOSE
    GUIDelete($Form1)
    ExitLoop
   Case $Button1
    GUIDelete($Form1)
    ExitLoop
  EndSwitch
WEnd
While 1
  If _IsPressed("01") Then
   ;code to get window from point
   Return $WndInfo
  EndIf
  Sleep(10)
WEnd
EndFunc   ;==>_GetWndInfo
Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

  • Moderators

JohnOne,

I see your problem - that needed to be integrated from the start. :)

However, you can easily switch to MessageLoop mode within that function and then back to OnEvent as you leave it:

Func _GetWndInfo()
    
    ; Switch to MessageLoop mode <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    Local $iOldOpt = Opt("GUIOnEventMode", 0)
    
    Local $WndInfo[9]
    $Form1 = GUICreate("Form1", 185, 119, 192, 124)
    $Button1 = GUICtrlCreateButton("Button1", 48, 56, 75, 25)
    $Label1 = GUICtrlCreateLabel("hover over the window and click", 16, 32, 158, 17)
    GUISetState(@SW_SHOW)
    
    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE, $Button1 ; You can have multiple arguments in a Switch ;)
                GUIDelete($Form1)
                ExitLoop
        EndSwitch
    WEnd
    While 1
        If _IsPressed("01") Then
            ;code to get window from point
            ; Change back to OnEvent mode <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            Opt("GUIOnEventMode", $iOldOpt)
            Return $WndInfo
        EndIf
        Sleep(10)
    WEnd
    
EndFunc   ;==>_GetWndInfo

I do this in several of my UDFs - as I have no idea of what mode the user is using. It is not something I would normally recommend within a single script as it is far better to keep the same mode throughout (UDFS are obviously a special case), but it will save you having to rewrite the whole thing this time. ;)

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

That's the weird thing M32, I tried this setting it to 0 on enter func and 1 on all possible exits

yet still the gui will not respond, so I just thought it was not possible ;)

J1

What do you think of my new sign :)

EDIT:

I know it works with some other options as I got the tip from yourself a while back

I think with wintitlematchmode, but assumed this was no good.

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

  • Moderators

JohnOne,

Switching modes is perfectly legal and should work without problem (if done correctly ;)). Do you want to post (or PM me) the whole thing so I can take a look? :)

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

I cannot believe it, I just copied and pasted "Opt("GUIOnEventMode", 0)" directly from the helpfile

and now the code is working as I first expected.

I just tried it with a spelling error, and got a scite error so it cannot have been that.

Think it's time for an early night.

Thank you M23 for your time and help, I feel like such a chump.

J1 :)

EDIT:

If you seen my code, your hat would fly off and steam would come out of your ears ;)

Thanks again Melba23

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

  • Moderators

JohnOne,

A pleasure as always. :)

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