Jump to content

Why does this GUI not close?


aommaster
 Share

Recommended Posts

Hey guys!

#include <GUIConstantsEx.au3>
#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <GuiEdit.au3>

global $switch=0

$defaultvalue=1

$guititle=" "
$childWin=GuiCreate($guititle,400,400, -1, -1, -1, -1)
    
GUISetOnEvent($GUI_EVENT_CLOSE, "closechild")
GuiCtrlCreateEdit($defaultvalue,5,5,390,350,$ES_MULTILINE)
    
$acceptinput=guictrlcreatebutton("Ok", 20, 355)
guictrlsetonevent(-1, "acceptinput")
    
$close=guictrlcreatebutton("Cancel", 50, 355)
guictrlsetonevent(-1, "closechild")
msgbox(0, "", "enter")
GuiSetState()
while $switch==0
    sleep(100)
WEnd
msgbox(0, "", "exit")
guidelete($childwin)


func closechild()
    $switch=1
    msgbox(0, "", "done")
    Return
EndFunc

func acceptinput()
    $switch=1
    msgbox(0, "", "done")
    Return
EndFunc

The window above doesn't seem to close. Now, I have made GUI's before and I do know that there is a much simpler way of doing it, however, this is not my whole code, and the code here needs to be as is (ie, setoneventmode on, etc.)

What am I doing wrong with this code?

Thanks guys

Link to comment
Share on other sites

Hi Zedna!

I tried adding that to the start of my actual script, but that doesn't seem to fix the problem (it does for the one I'd posted). I can't really post the script because it's more than 1000 lines of code.

Basically, I have a function that's in a loop and after a certain condition is met, it calls getuserinput(). Basically, this is what I want it to do:

Open up a window based on the variables that were passed on to it (guititle, and defaultvalue)

Wait for the user's input

Take the values in the edit box, and pass them back to the function that called it.

I decided to write up the functions as follows, but it doesn't seem to work (switch is a global variable):

func getuserinput($guititle, $defaultvalue)
    Opt('GUIOnEventMode',1)
    $switch=0
    $childWin=GuiCreate($guititle,400,400, -1, -1, -1, -1, $window)
    
    GUISetOnEvent($GUI_EVENT_CLOSE, "closechild")
    GuiCtrlCreateEdit($defaultvalue,5,5,390,350,$ES_MULTILINE)
    
    $acceptinput=guictrlcreatebutton("Ok", 20, 355)
    guictrlsetonevent(-1, "acceptinput")
    
    $close=guictrlcreatebutton("Cancel", 50, 355)
    guictrlsetonevent(-1, "closechild")
    msgbox(0, "", "enter")
    GuiSetState()
    while $switch==0
        sleep(100)
    WEnd
    msgbox(0, "", "exit")
    guidelete($childwin)
EndFunc

func closechild()
    $switch=1
    msgbox(0, "", "done")
    Return
EndFunc

func acceptinput()
    $switch=1
    msgbox(0, "", "done")
    Return
EndFunc
Link to comment
Share on other sites

Place it at start of your script as I said

#include <GUIConstantsEx.au3>
#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <GuiEdit.au3>

Opt('GUIOnEventMode',1)

global $switch=0

$defaultvalue=1

$guititle=" "
$childWin=GuiCreate($guititle,400,400, -1, -1, -1, -1)
    GUISetOnEvent($GUI_EVENT_CLOSE, "closechild")
GuiCtrlCreateEdit($defaultvalue,5,5,390,350,$ES_MULTILINE)
    $acceptinput=guictrlcreatebutton("Ok", 20, 355)
guictrlsetonevent(-1, "acceptinput")
    $close=guictrlcreatebutton("Cancel", 50, 355)
guictrlsetonevent(-1, "closechild")
msgbox(0, "", "enter")
GuiSetState()
while $switch==0
    sleep(100)
WEnd
msgbox(0, "", "exit")
guidelete($childwin)


func closechild()
    $switch=1
    msgbox(0, "", "done")
    Return
EndFunc

func acceptinput()
    $switch=1
    msgbox(0, "", "done")
    Return
EndFunc
Edited by Zedna
Link to comment
Share on other sites

Hi Zedna!

You didn't understand what I meant :)

The Opt('GUIOnEventMode',1) works for the script that I had posted, so that was all good. But when I put it into the actual code (the one that it more than 1000 lines long), it doesn't work.

If you take a look at my post #3, I tried to explain how the main program works.

Link to comment
Share on other sites

Hi Zedna!

I think I may have narrowed my problem down and was wondering whether you could give me a few more pointers (no pun intended).

My problem is that I have multiple GUI's on a SetOnEvent mode. How do I handle them using this specific mode, since I've already written more than 1000 lines of code, and changing the mode to getmessage would be a painful task?

Thanks a lot for your help!

Link to comment
Share on other sites

Hi Zedna!

I think I may have narrowed my problem down and was wondering whether you could give me a few more pointers (no pun intended).

My problem is that I have multiple GUI's on a SetOnEvent mode. How do I handle them using this specific mode, since I've already written more than 1000 lines of code, and changing the mode to getmessage would be a painful task?

Thanks a lot for your help!

I think you should be able to switch to onevent mode in a function the way you showed, but if the rest of the script is not using onevent mode then you need to have

Opt('GUIOnEventMode',0)

before you leave the function. If you switch between modes I don't know if the events set will be retained but I would guess they would. You'd have to try it, but in your example it doesn't look like it would be a problem.

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 think you should be able to switch to onevent mode in a function the way you showed, but if the rest of the script is not using onevent mode then you need to have

Opt('GUIOnEventMode',0)

before you leave the function. If you switch between modes I don't know if the events set will be retained but I would guess they would. You'd have to try it, but in your example it doesn't look like it would be a problem.

Hi Martin!

That was the first thing that came to mind when I encountered this problem. I posted another thread with an example code:

http://www.autoitscript.com/forum/index.php?showtopic=86781

However, it does seem like all the other predefined functions seem to deactivate.

The general form of my code is:

#includes

Global variables

GUIOnEventMode=1

Define the GUI along with the guictrlsetonevent

.

.

.

Functions with use gui oneventmode=1

The last function which looks like:

{

GUIOnEventMode=0

Stuff here

GUIOnEventMode=1

}

Which was the form you recommended I use, correct?

Because if that is the case, then, it doesn't work :)

Link to comment
Share on other sites

Hi Martin!

That was the first thing that came to mind when I encountered this problem. I posted another thread with an example code:

http://www.autoitscript.com/forum/index.php?showtopic=86781

However, it does seem like all the other predefined functions seem to deactivate.

The general form of my code is:

#includes

Global variables

GUIOnEventMode=1

Define the GUI along with the guictrlsetonevent

.

.

.

Functions with use gui oneventmode=1

The last function which looks like:

{

GUIOnEventMode=0

Stuff here

GUIOnEventMode=1

}

Which was the form you recommended I use, correct?

Because if that is the case, then, it doesn't work :)

I haven't found a problem switching from message loop to onevent mode and back.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Global $switchback = False
Global $gui, $background, $pic, $btn2

Global $g2done = False
Local $msg

$gui1 = GUICreate("My GUI"); will create a dialog box that when displayed is centered
$button1 = GUICtrlCreateButton("step 2", 20, 20, 100, 22)
GUISetState(@SW_SHOW); will display an empty dialog box

; Run the GUI until the dialog is closed
While 1
    $msg = GUIGetMsg()
    If $msg = $button1 Then
        MsgBox(262144, "message loop", "is working")
        Example2()
        $switchback = False
    EndIf
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd
GUIDelete()


; example 2
Func Example2()


    Opt("GuiOnEventMode", 1)
    If Not $g2done Then
        $gui = GUICreate("Background", 400, 100)
    ; background picture
        $background = GUICtrlCreatePic(@SystemDir & "\oobe\images\mslogo.jpg", 0, 0, 400, 100)
        GUICtrlSetState(-1, $Gui_Disable)
        $btn2 = GUICtrlCreateButton("goback", 20, 20, 100, 20)
        GUICtrlSetOnEvent(-1, "btn2Event")
        GUISetState(@SW_SHOW)
        $g2done = True
    Else
        GUISwitch($gui)
    EndIf


    Do
        Sleep(30)

    Until $switchback = True
;GUIDelete($gui)
    GUISwitch($gui1)
    Opt("GuiOnEventMode", 0)
EndFunc  ;==>Example2
;
Func btn2Event()
    MsgBox(262144, "event mode", "is working")
    $switchback = True
EndFunc  ;==>btn2Event

EDIT: changed the script to have the event set only once when the second window is created .

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

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