Jump to content

Passing GUI names to GUISetState


JBassman
 Share

Recommended Posts

OK folks,

I am working on a GUI for a prog I am building for work. I am trying to pass a gui name to GUISetState to toggle the GUI being visible or not. I have been through the help multiple times, and I have googled everything I can to try and find a solution. Since all that has come to a dead end I have decided to post to you guys to see if you can help me. I am sure it is something simple I am missing, but I just can't find it. Code is below. I have a main gui with buttons that open 3 child guis, and I'd like to have a button on the child guis that I can press to go back to the main gui. If you look in $Child_Button_1 below, you can see how I was trying to track which child was open and close it by creating the gui name of "$ChildX" and passing it to GUISetState, but it doesn't seem to work that way. So I guess my question is can this be done. If so, any help would be greatly appreciated.

Thanks,

J

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Global $aWinGetPos, $hChild, $hGUI, $Button_1, $Button_2, $Button_3, $Child_Button_1, $CurrentChild, $Label2, $Label3, $Label4
$hGUI = GUICreate("GUI Example", 600, 400, -1, -1)
$Button_1 = GUICtrlCreateButton("Child 1", 10, 10, 115, 25)
$Button_2 = GUICtrlCreateButton("Child 2", 130, 10, 115, 25)
$Button_3 = GUICtrlCreateButton("Child 3", 250, 10, 115, 25)
$Child_Button_1 = GUICtrlCreateButton("Back", 475, 370, 115, 25)
GUISetState(@SW_SHOW, $hGUI)
 
$Child1 = GUICreate("", 600, 300, -1, 50, $WS_POPUP, $WS_EX_MDICHILD, $hGUI)
GUISetBkColor(0xff)
$Label2 = GUICtrlCreateLabel("Click to add a new employ to the photo directory.", 130, 55, 234, 17)
;$Child_Button_1 = GUICtrlCreateButton("Back", -1, -1, 115, 25)
 
$Child2 = GUICreate("", 600, 300, -1, 50, $WS_POPUP, $WS_EX_MDICHILD, $hGUI)
GUISetBkColor(0xFF0000)
$Label3 = GUICtrlCreateLabel("Click to change a current employ's info in the photo directory.", 130, 85, 291, 17)
;$Child_Button_1 = GUICtrlCreateButton("Back", -1, -1, 115, 25)
 
$Child3 = GUICreate("", 600, 300, -1, 50, $WS_POPUP, $WS_EX_MDICHILD, $hGUI)
GUISetBkColor(0x009900)
$Label4 = GUICtrlCreateLabel("Click to remove an employ from the photo directory.", 130, 115, 245, 17)
 
While 1
 
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
  
  Case $Child_Button_1
   Msgbox(0,'Child', 'You pressed the back button on child' & $CurrentChild)
   $CloseMe = "$Child" & $CurrentChild
   Msgbox(0,'Child', 'Close: ' & $CloseMe)
   GUISetState(@SW_HIDE, $CloseMe)
   ;GUISetState(@SW_HIDE, $Child2)
   ;GUISetState(@SW_HIDE, $Child3)
 
  Case $Button_1
   GUISetState(@SW_SHOW, $Child1)
   GUISetState(@SW_HIDE, $Child2)
   GUISetState(@SW_HIDE, $Child3)
   $CurrentChild = 1
 
  Case $Button_2
   GUISetState(@SW_SHOW, $Child2)
   GUISetState(@SW_HIDE, $Child1)
   GUISetState(@SW_HIDE, $Child3)
   $CurrentChild = 2
 
  Case $Button_3
   GUISetState(@SW_SHOW, $Child3)
   GUISetState(@SW_HIDE, $Child2)
   GUISetState(@SW_HIDE, $Child1)
   $CurrentChild = 3
 
    EndSwitch
 
WEnd
Link to comment
Share on other sites

  • Moderators

JBassman,

Welcome to the AutoIt forum. :D

Alas $CloseMe = "$Child" & $CurrentChild will not work - it just produces a literal string "$Child1". :oops:

You need to use Execute to get the value of the variable - like this: :rip:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
 
Global $aWinGetPos, $hChild, $hGUI, $Button_1, $Button_2, $Button_3, $Child_Button_1, $CurrentChild, $Label2, $Label3, $Label4
 
$hGUI = GUICreate("GUI Example", 600, 400, -1, -1)
 
$Button_1 = GUICtrlCreateButton("Child 1", 10, 10, 115, 25)
$Button_2 = GUICtrlCreateButton("Child 2", 130, 10, 115, 25)
$Button_3 = GUICtrlCreateButton("Child 3", 250, 10, 115, 25)
$Child_Button_1 = GUICtrlCreateButton("Back", 475, 370, 115, 25)
 
GUISetState(@SW_SHOW, $hGUI)
 
$Child1 = GUICreate("", 600, 300, -1, 50, $WS_POPUP, $WS_EX_MDICHILD, $hGUI)
GUISetBkColor(0xff)
$Label2 = GUICtrlCreateLabel("Click to add a new employ to the photo directory.", 130, 55, 234, 17)
$Child_Button_1 = GUICtrlCreateButton("Back", 130, 100, 115, 25)
GUISetState(@SW_HIDE, $Child1)
 
$Child2 = GUICreate("", 600, 300, -1, 50, $WS_POPUP, $WS_EX_MDICHILD, $hGUI)
GUISetBkColor(0xFF0000)
$Label3 = GUICtrlCreateLabel("Click to change a current employ's info in the photo directory.", 130, 85, 291, 17)
GUISetState(@SW_HIDE, $Child2)
 
$Child3 = GUICreate("", 600, 300, -1, 50, $WS_POPUP, $WS_EX_MDICHILD, $hGUI)
GUISetBkColor(0x009900)
$Label4 = GUICtrlCreateLabel("Click to remove an employ from the photo directory.", 130, 115, 245, 17)
GUISetState(@SW_HIDE, $Child3)
 
While 1
 
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
 
        Case $Child_Button_1
            MsgBox(0, 'Child', 'You pressed the back button on child' & $CurrentChild)
            GUISetState(@SW_HIDE, Execute("$Child" & $CurrentChild)) ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
        Case $Button_1
            GUISetState(@SW_SHOW, $Child1)
            GUISetState(@SW_HIDE, $Child2)
            GUISetState(@SW_HIDE, $Child3)
            $CurrentChild = 1
 
        Case $Button_2
            GUISetState(@SW_SHOW, $Child2)
            GUISetState(@SW_HIDE, $Child1)
            GUISetState(@SW_HIDE, $Child3)
            $CurrentChild = 2
 
        Case $Button_3
            GUISetState(@SW_SHOW, $Child3)
            GUISetState(@SW_HIDE, $Child2)
            GUISetState(@SW_HIDE, $Child1)
            $CurrentChild = 3
 
    EndSwitch
 
WEnd

But as the button can only be pressed when that GUI is visible, why not use the variable $Child1 directly? I see no advantage in going about it the long way round by composing it and using Execute. :)

I hope this helps - please ask if you have any questions. :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

  • Moderators

JBassman,

Just use the variable directly like this:

GUISetState(@SW_HIDE, $Child1)

The variable exists because you have already created the GUI and there is no need to work out which child is visible as the button is only available to be pressed if that particular GUI is visible. :oops:

All clear? :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

  • Moderators

JBassman,

Glad I could help. :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

Here's my take on it:

I changed a small part of the script to what would need to be changed.

MsgBox(0, 'Child', 'You pressed the back button on child' & $CurrentChild)
   $CloseMe = Eval("Child" & $CurrentChild) ;  <<<<<<<<<<< change this line
   MsgBox(0, 'Child', 'Close: ' & $CloseMe)

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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