Jump to content

Multiple Gui with GUICtrlSetOnEvent


Pook
 Share

Recommended Posts

Okay, I’ve been looking around the forums and reading the Help file. (I must not be looking in the wrong place, or it’s just one of those days).

I have a script that opens up a form ($Form_1) with buttons that users can pick from. If a user pick ‘button A’ then the main form ($Form_1) will close (using the func below). The new Gui form ($Form_2) opens fine, however none of the ‘buttons’ (GUICtrlSetOnEvent) commands will work under the new GUI. Does this make sense?

func _switch_Console1()

GUIDelete($Form_1)
_MainC() ; opens new ($Form_2)

endfunc
Link to comment
Share on other sites

I have the GUICtrlSetOnEvent commands for the new GUI ($Form_2) inside the Func command (_MainC). (Not apart of $Form_1 code) How to you reset all guievents? (is that in the help file?

Not sure if it matters but I'm using the code below to on both forms to keep the GUI open

While 1
    sleep(10) 
WEnd
Link to comment
Share on other sites

Maybe this will help clear things up

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
Global $gui, $gui2
Example1()
while WinExists("My GUI")
    sleep(100)
WEnd
Example2()
while WinExists("My GUI")
    sleep(100)
WEnd

; example 1
Func Example1()
   $gui=GUICreate("My GUI") ; will create a dialog box that when displayed is centered
    GUISetOnEvent($GUI_EVENT_CLOSE, "close")
    GUICtrlCreateButton("Hi",100,100,100,100)
    GUICtrlSetOnEvent(-1,"_pressed")
    GUISetState(@SW_SHOW) ; will display an empty dialog box
EndFunc   ;==>Example1

; example 2
Func Example2()
    Local $gui, $background, $pic, $basti_stay, $msg
    Local $sFile = "..\GUI\logo4.gif"
    $gui2 = GUICreate("My GUI", 400, 300)
        GUISetOnEvent($GUI_EVENT_CLOSE, "close2")
        GUICtrlCreateButton("Bye",100,100,100,100)
        GUICtrlSetOnEvent(-1,"_pressed")
    GUISetState(@SW_SHOW)
EndFunc   ;==>Example2

func close()
GUIDelete($gui)
EndFunc

func close2()
GUIDelete($gui2)
EndFunc

func _pressed()
    MsgBox(0,"", "Button Pressed")
EndFunc
Link to comment
Share on other sites

This is what I'm doing and it doesn't work. I lost.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
Global $gui, $gui2

_Example1()

; example 1
Func _Example1()
   $gui=GUICreate("My GUI") ; will create a dialog box that when displayed is centered
GUISetOnEvent($GUI_EVENT_CLOSE, "_exit")
    $Button = GUICtrlCreateButton("Hi",100,100,100,100)
    GUISetState(@SW_SHOW) ; will display an empty dialog box
    
    GUICtrlSetOnEvent($Button,"_pressed")
    
    While 1
    sleep(10) ; Comment this line out to see difference in cpu Usage.
    WEnd
    
EndFunc   ;==>Example1

; example 2
Func _Example2()
    Local $gui, $background, $pic, $basti_stay, $msg
    Local $sFile = "..\GUI\logo4.gif"
    $gui2 = GUICreate("My GUI", 400, 300)
        GUISetOnEvent($GUI_EVENT_CLOSE, "_exit")
        $Button2 = GUICtrlCreateButton("Bye",100,100,100,100)
           

    GUISetState(@SW_SHOW)
    
  GUICtrlSetOnEvent($Button2,"_exit") 

    
  While 1
    sleep(10) ; Comment this line out to see difference in cpu Usage.
  WEnd
    
EndFunc   ;==>Example2


func _pressed()
GUIDelete($gui)
_Example2()
EndFunc

func _exit()
GUIDelete($gui2)
exit
EndFunc
Link to comment
Share on other sites

You should never stay in any event function longer than absolutely necessary. This works:

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

Opt("GUIOnEventMode", 1)
Global $gui, $gui2

_Example1()

While 1
    Sleep(10) ; Comment this line out to see difference in cpu Usage.
WEnd

; example 1
Func _Example1()
    $gui = GUICreate("My GUI") ; will create a dialog box that when displayed is centered
    GUISetOnEvent($GUI_EVENT_CLOSE, "_exit", $gui)
    GUICtrlCreateButton("Hi", 100, 100, 100, 100)
    GUICtrlSetOnEvent(-1, "_pressed")
    GUISetState(@SW_SHOW) ; will display an empty dialog box
EndFunc   ;==>_Example1

; example 2
Func _Example2()
    $gui2 = GUICreate("My GUI", 400, 300)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_exit", $gui2)
    GUICtrlCreateButton("Bye", 100, 100, 100, 100)
    GUICtrlSetOnEvent(-1, "_exit")
    GUISetState(@SW_SHOW)
EndFunc   ;==>_Example2


Func _pressed()
    ConsoleWrite("Debug:  _pressed() called" & @LF)
    GUIDelete($gui)
    _Example2()
EndFunc   ;==>_pressed

Func _exit()
    ConsoleWrite("Debug:  _exit() called" & @LF)
    GUIDelete($gui2)
    Exit
EndFunc   ;==>_exit

:idea:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Really, as an alternative

I would use the original GUI, instead of deleting and remaking. Something like this.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <WinAPI.au3>
Opt("GUIOnEventMode", 1)
Global $gui


_Example1()

While 1
    sleep(10) ; Comment this line out to see difference in cpu Usage.
WEnd

; example 1
Func _Example1()
   $gui=GUICreate("My GUI") ; will create a dialog box that when displayed is centered
GUISetOnEvent($GUI_EVENT_CLOSE, "_exit", $gui)
    $Button = GUICtrlCreateButton("Hi",100,100,100,100)
    GUISetState(@SW_SHOW) ; will display an empty dialog box
    GUICtrlSetOnEvent($Button,"_pressed")
EndFunc   ;==>Example1

; example 2
Func _Example2()
    WinMove("My GUI","", 400, 300)
        $Button2 = GUICtrlCreateButton("Bye",100,100,100,100)
           GUICtrlSetOnEvent($Button2,"_exit") 
    GUISetState(@SW_SHOW)
EndFunc   ;==>Example2


func _pressed()
deletecontrols(-1,$gui)
_Example2()
EndFunc

func _exit()
exit 0
EndFunc

Func deletecontrols($last=-1,$guiname="")
    $aLastID = DllCall("user32.dll", "int", "GetDlgCtrlID", "hwnd", GUICtrlGetHandle($last))
    For $i = 0 To $aLastID[0]
        if WinGetHandle($guiname)=_WinAPI_GetParent(GUICtrlGetHandle($i)) Then
        GUICtrlDelete($i)
        EndIf
    Next
EndFunc
Edited by picea892
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...