Jump to content

Recommended Posts

Posted

Hi,

I have a simple gui with a button which opens a second window, then when you close the 'child gui' it should just go back to the main gui.

here is the script...

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#NoTrayIcon
#RequireAdmin
Opt("GUIOnEventMode", 1)
Opt("GUICloseOnESC", 1)

Local $Var = False

$Form1 = GUICreate("Main", 456, 313, -1, -1)
$LABEL = GUICtrlCreateLabel("Child Window Has not been opened", 80, 40, 310, 28)
GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000000)
GUISetState(@SW_SHOW)
GUISetOnEvent($GUI_EVENT_CLOSE, "_CloseApplication")

;Buttons
$BUTTON_CHILD = GUICtrlCreateButton("Child", 184, 144, 75, 25)
GUICtrlSetOnEvent($BUTTON_CHILD, "_Begin_Open_Function")

While 1
Sleep(0)
If $Var Then
        _Open_Function()
    EndIf
WEnd

;Close Func
Func _CloseApplication()
    Exit
EndFunc

;Child Window Func
Func _Begin_Open_Function()
$Var = True
EndFunc

Func _Open_Function()
$Var = False
Opt("GUIOnEventMode", 1)
Opt("GUICloseOnESC", 1)
GUISetState(@SW_DISABLE, $Form1)
$FormChild = GUICreate("Child", 320, 200, -1, -1)
GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000000)
GUISetState(@SW_SHOW, $FormChild)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Close_Child_Window")
EndFunc

;Close Child Window
Func _Close_Child_Window()
GUISetState(@SW_ENABLE, $Form1)
GUIDelete($FormChild)
EndFunc

What it's doing is when you close the child gui it says "$FormChild variable being used without being declaired" and I cant see why its doing this. Can anyone tell me please?

Thanks

Posted (edited)

I made one or two changes to your code.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#NoTrayIcon
#RequireAdmin
Opt("GUIOnEventMode", 1)
Opt("GUICloseOnESC", 1)
Global $FormChild, $Var = False

$Form1 = GUICreate("Main", 456, 313, -1, -1)
$LABEL = GUICtrlCreateLabel("Child Window Has not been opened", 80, 40, 310, 28)
GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000000)

;Buttons
$BUTTON_CHILD = GUICtrlCreateButton("Child", 184, 144, 75, 25)
GUISetState(@SW_SHOW)

GUICtrlSetOnEvent($BUTTON_CHILD, "_Begin_Open_Function")
GUISetOnEvent($GUI_EVENT_CLOSE, "_CloseApplication")

While 1
    Sleep(0)
    If $Var Then
        _Open_Function()
    EndIf
WEnd

;Close Func
Func _CloseApplication()
    Exit
EndFunc

;Child Window Func
Func _Begin_Open_Function()
    $Var = True
EndFunc

Func _Open_Function()
    ;Opt("GUIOnEventMode", 1)
    ;Opt("GUICloseOnESC", 1)
    GUISetState(@SW_DISABLE, $Form1)
    $FormChild = GUICreate("Child", 320, 200, -1, -1)
    GUICtrlSetFont($FormChild, 14, 400, 0, "MS Sans Serif")
    GUICtrlSetColor($FormChild, 0x000000)
    GUISetState(@SW_SHOW, $FormChild)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Close_Child_Window")
    $Var = False
EndFunc

;Close Child Window
Func _Close_Child_Window()
    GUISetState(@SW_ENABLE, $Form1)
    GUIDelete($FormChild)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_CloseApplication")
EndFunc

You should look at the excellent examples by Melba23 in the Wiki. => Managing Multiple GUIs.

Edited by czardas
  • Moderators
Posted

am632,

The $FormChild variable is only declared in the _Open_Function function when it is created. As you do not specify a scope, AutoIt defaults to the Local scope and the variable is invisible to any other function in the script - so when you try to access it in the _Close_Child_Window function, you get the error. Solution: add the $FormChild variable as Global at the top of your script. :mellow:

By the way, the Local $Var = False is automatically recast as all variables in the main script are Global in scope. And you do not need to repeat the Opt lines in the _Open_Function function - the initial ones are valid for the whole script. :)

All clear? :)

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

 

  • Moderators
Posted

czardas,

Delighted someone finds it useful. :mellow:

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

 

Posted (edited)

czardas,

Delighted someone finds it useful. :mellow:

M23

No need to feel embarrassed about it. I'm not the only one to have benefited from your experience and knowledge. Just keep it up, and thanks for sharing it with us. :) Edited by czardas

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...