jazzyjeff Posted September 30, 2010 Posted September 30, 2010 Hello, I have tried many different ways of reorganising my code to get this to work, and I am at a loss now as to what to do. I have a menu from the SysTray Icon and one of the items brings up a small GUI. I have a button in the GUI that I would like to close the GUI, but keep the script running. At present I have a Save button that I want to some settings to an INI file, and then close the GUI. I have achieved this by setting the GUI state to @SW_HIDE. It doesn't seem ideal really as the GUI is still there, so maybe the follow up questions will in turn help me find a better solution for this. I have 2nd button in this GUI that then opens up another GUI with a Close button. I want this Close button to just close that GUI and then Return to the 1st GUI. I can't figure it out. I thought that perhaps if I have 2 While Statements in the program they would then be in their own loops, but this doesn't do anything. I think I am looking at the problem all wrong, so I am hoping someone can make this clearer to understand how I achieve my goal. Here is the code that I currently have: expandcollapse popup#include <TCP.au3> #include <WindowsConstants.au3> #include <GUIConstantsEx.au3> #NoTrayIcon Opt("TrayMenuMode",1) ; Server Settings Function Variables Global $btnSaveServer,$btnViewServer,$btnClose,$IPAddress,$Port,$sIPAddress,$sPort,$iPort,$iIPAddress $settingsItem = TrayCreateMenu("Settings") $serverItem = TrayCreateItem("Server", $settingsItem) TrayCreateItem("") $aboutItem = TrayCreateItem("About") TrayCreateItem("") $exitItem = TrayCreateItem("Exit") TraySetState() While 1 $msg = TrayGetMsg() $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch Select Case $msg = 0 ContinueLoop Case $msg = $aboutItem Msgbox(64,"School Client","Version 1.0") Case $msg = $exititem ExitLoop Case $msg = $serverItem _ServerSettings() EndSelect WEnd While 2 Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $btnSaveServer $sIPAddress = GUICtrlRead($IPAddress) $sPort = GUICtrlRead($Port) ;_ProgDirChk() If FileExists("C:\School\Client\config.ini") Then IniWriteSection("C:\School\Client\config.ini","Server Config","IP=" & $sIPAddress & @LF & "Port=" & $sPort) Else IniWrite("C:\School\Client\config.ini","Server Config","IP",$sIPAddress) IniWriteSection("C:\School\Client\config.ini","Server Config","Port=" & $sPort) EndIf ExitLoop Case $btnViewServer $iIPAddress = IniRead("C:\School\Client\config.ini","Server Config","IP","") $iPort = IniRead("C:\School\Client\config.ini","Server Config","Port","") _ViewServerSettings() Case $btnClose ExitLoop EndSwitch WEnd Func _ProgDirChk() If Not FileExists("C:\School\Client") Then DirCreate("C:\School\Client\") EndIf EndFunc Func _ServerSettings() $formServer = GUICreate("Server Settings", 221, 107, @DesktopWidth - 250, @DesktopHeight - 200,$WS_POPUP) $lblServerIP1 = GUICtrlCreateLabel("IP Address", 16, 16, 55, 17) $lblServePort1 = GUICtrlCreateLabel("Port Number", 16, 48, 63, 17) $IPAddress = GUICtrlCreateInput("", 88, 13, 121, 21) $Port = GUICtrlCreateInput("", 88, 45, 121, 21) $btnViewServer = GUICtrlCreateButton("View", 50, 73, 75, 25, $WS_GROUP) $btnSaveServer = GUICtrlCreateButton("Save", 133, 73, 75, 25, $WS_GROUP) GUISetState(@SW_SHOW) EndFunc Func _ViewServerSettings() $formServerView = GUICreate("Server Settings", 221, 107, @DesktopWidth - 250, @DesktopHeight - 200,$WS_POPUP) $lblServerIP2 = GUICtrlCreateLabel("IP Address", 16, 16, 55, 17) $lblServePort2 = GUICtrlCreateLabel("Port Number", 16, 48, 63, 17) $lblIPAddress = GUICtrlCreateLabel($iIPAddress, 88, 13, 121, 21,$WS_POPUP) GUISetFont("",800) $lblPort = GUICtrlCreateLabel($iPort, 88, 45, 121, 21,$WS_POPUP) GUISetFont("",800) $btnClose = GUICtrlCreateButton("Close", 133, 73, 75, 25, $WS_GROUP) GUISetState(@SW_SHOW) EndFunc Thanks. Jeff
Tvern Posted September 30, 2010 Posted September 30, 2010 (edited) Take a look in the helpfile for While. While 1 doesn't mean "the first While loop" and While 2 doesn't mean "the second While loop" Instead 1 and 2 are both expressions that are always true, meaning either loop will not exit unless ExitLoop is reached.Instead of trying to run two loops at the same time, you need to do everything you want to do in one loop. Here is an example: Second one is better imo.expandcollapse popup;~ #include <TCP.au3> #include <WindowsConstants.au3> #include <GUIConstantsEx.au3> #NoTrayIcon Opt("TrayMenuMode",1) ; Server Settings Function Variables Global $btnSaveServer,$btnViewServer,$btnClose,$IPAddress,$Port,$sIPAddress,$sPort,$iPort,$iIPAddress $settingsItem = TrayCreateMenu("Settings") $serverItem = TrayCreateItem("Server", $settingsItem) TrayCreateItem("") $aboutItem = TrayCreateItem("About") TrayCreateItem("") $exitItem = TrayCreateItem("Exit") TraySetState() While 1 ;one loop checks for events from the gui, as well as the trayicon $msg = TrayGetMsg() Select ;removed the continueloop, as it would skip the gui part of the loop Case $msg = $aboutItem Msgbox(64,"School Client","Version 1.0") Case $msg = $exititem ExitLoop Case $msg = $serverItem _ServerSettings() EndSelect Switch GUIGetMsg() Case 0 ContinueLoop ;added continueloop to avoid false button presses. Case $GUI_EVENT_CLOSE Exit Case $btnSaveServer $sIPAddress = GUICtrlRead($IPAddress) $sPort = GUICtrlRead($Port) ;_ProgDirChk() If FileExists("C:\School\Client\config.ini") Then IniWriteSection("C:\School\Client\config.ini","Server Config","IP=" & $sIPAddress & @LF & "Port=" & $sPort) Else IniWrite("C:\School\Client\config.ini","Server Config","IP",$sIPAddress) IniWriteSection("C:\School\Client\config.ini","Server Config","Port=" & $sPort) EndIf ExitLoop Case $btnViewServer $iIPAddress = IniRead("C:\School\Client\config.ini","Server Config","IP","") $iPort = IniRead("C:\School\Client\config.ini","Server Config","Port","") _ViewServerSettings() Case $btnClose ExitLoop EndSwitch WEnd Func _ProgDirChk() If Not FileExists("C:\School\Client") Then DirCreate("C:\School\Client\") EndIf EndFunc Func _ServerSettings() $formServer = GUICreate("Server Settings", 221, 107, @DesktopWidth - 250, @DesktopHeight - 200,$WS_POPUP) $lblServerIP1 = GUICtrlCreateLabel("IP Address", 16, 16, 55, 17) $lblServePort1 = GUICtrlCreateLabel("Port Number", 16, 48, 63, 17) $IPAddress = GUICtrlCreateInput("", 88, 13, 121, 21) $Port = GUICtrlCreateInput("", 88, 45, 121, 21) $btnViewServer = GUICtrlCreateButton("View", 50, 73, 75, 25, $WS_GROUP) $btnSaveServer = GUICtrlCreateButton("Save", 133, 73, 75, 25, $WS_GROUP) GUISetState(@SW_SHOW) EndFunc Func _ViewServerSettings() $formServerView = GUICreate("Server Settings", 221, 107, @DesktopWidth - 250, @DesktopHeight - 200,$WS_POPUP) $lblServerIP2 = GUICtrlCreateLabel("IP Address", 16, 16, 55, 17) $lblServePort2 = GUICtrlCreateLabel("Port Number", 16, 48, 63, 17) $lblIPAddress = GUICtrlCreateLabel($iIPAddress, 88, 13, 121, 21,$WS_POPUP) GUISetFont("",800) $lblPort = GUICtrlCreateLabel($iPort, 88, 45, 121, 21,$WS_POPUP) GUISetFont("",800) $btnClose = GUICtrlCreateButton("Close", 133, 73, 75, 25, $WS_GROUP) GUISetState(@SW_SHOW) EndFuncEdit: I think this is a little nicer. It keeps running when you close the GUI like you asked and it destroys the GUI instead of hiding it, although personally I prefer hiding it.expandcollapse popup;~ #include <TCP.au3> #include <WindowsConstants.au3> #include <GUIConstantsEx.au3> #NoTrayIcon Opt("TrayMenuMode", 1) ; Server Settings Function Variables Global $btnSaveServer, $btnViewServer, $btnClose, $IPAddress, $Port ;less globals, more win! $settingsItem = TrayCreateMenu("Settings") $serverItem = TrayCreateItem("Server", $settingsItem) TrayCreateItem("") $aboutItem = TrayCreateItem("About") TrayCreateItem("") $exitItem = TrayCreateItem("Exit") TraySetState() While 1 ;one loop to check both UI's Switch TrayGetMsg() Case $aboutItem TrayItemSetState($aboutItem,4) ;unchecks menu items MsgBox(64, "School Client", "Version 1.0") Case $exitItem Exit Case $serverItem TrayItemSetState($serverItem,4) _ServerSettings() EndSwitch Switch GUIGetMsg() Case 0 ContinueLoop Case $GUI_EVENT_CLOSE, $btnClose GUIDelete() ;guideletes like you asked. Case $btnSaveServer GUIDelete() _SaveServerSettings() Case $btnViewServer GUIDelete() _ViewServerSettings() EndSwitch WEnd Func _ProgDirChk() If Not FileExists("C:\School\Client") Then DirCreate("C:\School\Client\") EndIf EndFunc Func _SaveServerSettings() GUISetState(@SW_HIDE) Local $sIPAddress = GUICtrlRead($IPAddress) ;local instead of global Local $sPort = GUICtrlRead($Port) ;_ProgDirChk() If FileExists("C:\School\Client\config.ini") Then IniWriteSection("C:\School\Client\config.ini", "Server Config", "IP=" & $sIPAddress & @LF & "Port=" & $sPort) Else IniWrite("C:\School\Client\config.ini", "Server Config", "IP", $sIPAddress) IniWriteSection("C:\School\Client\config.ini", "Server Config", "Port=" & $sPort) EndIf EndFunc Func _ServerSettings() GUICreate("Server Settings", 221, 107, @DesktopWidth - 250, @DesktopHeight - 200, $WS_POPUP) GUICtrlCreateLabel("IP Address", 16, 16, 55, 17) ;no need to save these ctrlID's GUICtrlCreateLabel("Port Number", 16, 48, 63, 17) $IPAddress = GUICtrlCreateInput("", 88, 13, 121, 21) $Port = GUICtrlCreateInput("", 88, 45, 121, 21) $btnViewServer = GUICtrlCreateButton("View", 50, 73, 75, 25, $WS_GROUP) $btnSaveServer = GUICtrlCreateButton("Save", 133, 73, 75, 25, $WS_GROUP) GUISetState(@SW_SHOW) EndFunc Func _ViewServerSettings() GUICreate("Server Settings", 221, 107, @DesktopWidth - 250, @DesktopHeight - 200, $WS_POPUP) GUISetFont("", 800) GUICtrlCreateLabel("IP Address", 16, 16, 55, 17) GUICtrlCreateLabel("Port Number", 16, 48, 63, 17) Local $iIPAddress = IniRead("C:\School\Client\config.ini", "Server Config", "IP", "") GUICtrlCreateLabel($iIPAddress, 88, 13, 121, 21, $WS_POPUP) Local $iPort = IniRead("C:\School\Client\config.ini", "Server Config", "Port", "") GUICtrlCreateLabel($iPort, 88, 45, 121, 21, $WS_POPUP) $btnClose = GUICtrlCreateButton("Close", 133, 73, 75, 25, $WS_GROUP) GUISetState(@SW_SHOW) EndFunc Edited September 30, 2010 by Tvern
jazzyjeff Posted September 30, 2010 Author Posted September 30, 2010 Thank you very much for the reply. You've explained a lot. The comments were very helpful in understanding what is happening.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now