Jump to content

*Problem* Select Loops Duping GUIs *Problem*


Go to solution Solved by BrewManNH,

Recommended Posts

Hello Everyone :bye: ,

My name is Will and I have used AutoIt for quite some time now to make mini apps for myself such as Video Game Bibles(Game Encyclopedias), Desktop/Folder Locks and other goofy projects. I am currently working on a little "reminder" project to keep things in mind since my memory is very selective :sweating: . So to get to my point I am having trouble with swapping GUIs when using a Select Loop nested inside of a While Loop. I have one GUI create initially and then the loop begins. It waits for buttons to be pressed but after adding the Btn_Min (Button to Minimize) Case things get screwy. After adding this Case then running my program the GUI is created/deleted infinitely. Now I do have the function to create the other GUI deleting the current one which is why the GUIs do not pile up but rather re-create. I have searched a bit to find out if there have been similar issues but I have not been able to find anything on it (probably because I don't know exactly what to search) but if anyone could point me in the correct direction it would be much appreciated! :)

 

Here is my script:

(Lines 28 & 29 are commented out. Those are the ones that cause the issue. Can press ESC Button for an emergency exit)

;Includes
#include <GUIConstantsEx.au3>

;HotKeys
HotKeySet("{ESC}", "QUIT")

;Globals:
Global $WinWid = 600, $WinHgt = 250, $WinWidMin = 100, $WinHgtMin = 15
Global $WinMinX = @DesktopWidth - ($WinWidMin + 50), $WinMinY = @DesktopHeight - ($WinHgtMin + 100)
Global $WinX = @DesktopWidth - ($WinWid + 50), $WinY = @DesktopHeight - ($WinHgt + 100)
Global $Min_Win, $Btn_Max, $Max_Win, $Btn_Min

;Start Program
Mini_Calender()

;Create Start Window & Main Function
Func Mini_Calender()

   Min_Win() ;Runs Min_Win() Function (Creates Mini Window)

   While 1 ;Begins While Loop

      $msg = GUIGetMsg() ;Watches GUI Events

      Select ;Begins Select Loop
      Case $msg = $Btn_Max ;If Max(<) Button Pressed
         Max_Win() ;Runs Max_Win() Function (Creates Full Window)
;     Case $msg = $Btn_Min ;If Min(>) Button Pressed
;        Min_Win() ;Runs Min_Win() Function (Creates Mini Window)
      Case $msg = $GUI_EVENT_CLOSE ;If Close(X) Button Pressed
         Exit ;Close Program
      EndSelect ;Ends Select Loop

   WEnd ;Ends While Loops

EndFunc

;Creates Mini Window
Func Min_Win()
   GUIDelete() ;Deletes Current GUI Window
   $Min_Win = GUICreate("Mini Calender", $WinWidMin, $WinHgtMin, $WinMinX, $WinMinY) ;Creates Mini Window
   $Btn_Max = GUICtrlCreateButton("<", 25, 0, 50, 15) ;Creates Max Button
   GUISetState(@SW_SHOW) ;Makes GUI Window Visible
EndFunc

;Creates Max Window
Func Max_Win()
   GUIDelete() ;Deletes Current GUI Window
   $Max_Win = GUICreate("Mini Calender", $WinWid, $WinHgt, $WinX, $WinY) ;Creates Max Window
   $Btn_Min = GUICtrlCreateButton(">", $WinWid - 75, $WinHgt - 16, 50, 15) ;Creates Min Button
   GUISetState(@SW_SHOW) ;Makes GUI Window Visible
EndFunc

;Emergency Exit (ESC Key)
Func QUIT()
   Exit ;Exits The Program
EndFunc

If you have any questions please ask me and I will do my best to answer.

Link to comment
Share on other sites

  • Solution

$Btn_Min has been declared but contains nothing, or in other words its contents = "0". GUIGetMsg returns 0 when nothing is happening with any controls on the GUI. So because $Btn_Min is 0 until you run the Max_Win function, the Case for $Btn_Min is always going to fire off. The easiest way to handle that in your script is to declare $Btn_Min with a value, such as 9999 or some other unlikely number that isn't a control ID before you try to run the Select loop.

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

$Btn_Min has been declared but contains nothing, or in other words its contents = "0". GUIGetMsg returns 0 when nothing is happening with any controls on the GUI. So because $Btn_Min is 0 until you run the Max_Win function, the Case for $Btn_Min is always going to fire off. The easiest way to handle that in your script is to declare $Btn_Min with a value, such as 9999 or some other unlikely number that isn't a control ID before you try to run the Select loop.

 

That sure enough fixed it. :thumbsup: Thank You for the swift reply! I supposed I'm confused on how the GUIMsg/Select Loop/GUICtrlCreate mechanics work together. Such as when the loop runs why is there even an issue until the $Btn_Min is even clicked by the user etc. If the Select Loop is waiting for the GUIMsg to match up with something then why is $Btn_Min even an issue when it does not exist yet?

No need to explain it to me unless you really wish to. I'll read up on this more on my own :) .

Link to comment
Share on other sites

You declared it as a global variable at the start of your script, so the variable exists it's just that the button you want to assign that variable to doesn't yet exist. As I said, GUIGetMsg returns 0 when there's nothing going on in your GUI, and because $Btn_Min wasn't assigned a value yet it equals zero. So when there's nothing happening on your GUI, $msg equals 0 (from the return of GUIGetMsg), and with $Btn_Min also equalling 0, the Select/Case for $Btn_Min is fired.

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

You declared it as a global variable at the start of your script, so the variable exists it's just that the button you want to assign that variable to doesn't yet exist. As I said, GUIGetMsg returns 0 when there's nothing going on in your GUI, and because $Btn_Min wasn't assigned a value yet it equals zero. So when there's nothing happening on your GUI, $msg equals 0 (from the return of GUIGetMsg), and with $Btn_Min also equalling 0, the Select/Case for $Btn_Min is fired.

 

Oh jeez I'm sorry lol I understand now. When 0 is Returned the Select Loop ends. So when the loop hit $Btn_Min (Equaling 0) it would just exit and While Loop back again... I feel so dumb haha. Well I fixed it by running the Max_Win() function to initialize it right before running Min_Win(). Thank You so much for giving me some of your time. Any idea on why the $Btn_Min won't work correctly now? If I start it -> Click $Btn_Max -> Click $Btn_Min it only continues to recreate the Max Win.

Here's the Updated Code. (Just added the Max_Win() prior to the initial Min_Win())

;Includes
#include <GUIConstantsEx.au3>

;HotKeys
HotKeySet("{ESC}", "QUIT")

;Globals:
Global $WinWid = 600, $WinHgt = 250, $WinWidMin = 100, $WinHgtMin = 15
Global $WinMinX = @DesktopWidth - ($WinWidMin + 50), $WinMinY = @DesktopHeight - ($WinHgtMin + 100)
Global $WinX = @DesktopWidth - ($WinWid + 50), $WinY = @DesktopHeight - ($WinHgt + 100)
Global $Min_Win, $Btn_Max, $Max_Win, $Btn_Min

;Start Program
Mini_Calender()

;Create Start Window & Main Function
Func Mini_Calender()
   Max_Win()
   Min_Win() ;Runs Min_Win() Function (Creates Mini Window)

   While 1 ;Begins While Loop

      $msg = GUIGetMsg() ;Watches GUI Events

      Select ;Begins Select Loop
      Case $msg = $Btn_Max ;If Max(<) Button Pressed
         Max_Win() ;Runs Max_Win() Function (Creates Full Window)
      Case $msg = $Btn_Min ;If Min(>) Button Pressed
         Min_Win() ;Runs Min_Win() Function (Creates Mini Window)
      Case $msg = $GUI_EVENT_CLOSE ;If Close(X) Button Pressed
         Exit ;Close Program
      EndSelect ;Ends Select Loop

   WEnd ;Ends While Loops

EndFunc

;Creates Mini Window
Func Min_Win()
   GUIDelete() ;Deletes Current GUI Window
   $Min_Win = GUICreate("Mini Calender", $WinWidMin, $WinHgtMin, $WinMinX, $WinMinY) ;Creates Mini Window
   $Btn_Max = GUICtrlCreateButton("<", 25, 0, 50, 15) ;Creates Max Button
   GUISetState(@SW_SHOW) ;Makes GUI Window Visible
EndFunc

;Creates Max Window
Func Max_Win()
   GUIDelete() ;Deletes Current GUI Window
   $Max_Win = GUICreate("Mini Calender", $WinWid, $WinHgt, $WinX, $WinY) ;Creates Max Window
   $Btn_Min = GUICtrlCreateButton(">", $WinWid - 75, $WinHgt - 16, 50, 15) ;Creates Min Button
   GUISetState(@SW_SHOW) ;Makes GUI Window Visible
EndFunc

;Emergency Exit (ESC Key)
Func QUIT()
   Exit ;Exits The Program
EndFunc
Edited by Pickpocketz88
Link to comment
Share on other sites

When you delete the GUI you're deleting the controls inside them, but you're not changing the variables that the control IDs were assigned to. When you create the Min_Win GUI, the $Btn_Max control ID is assigned the value of 3. When you delete that GUI going into the Max_Win function, $Btn_Min's control is created with the same Control ID of 3. So even though keep trying to minimize the GUI with the minimize button, all you're doing is calling the wrong function because both variables contain the same control ID now.

What you should be doing, instead of deleting the GUI's, is create both of them, but hide one of them at a time with the buttons, that way the control ID's stay valid.

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

When you delete the GUI you're deleting the controls inside them, but you're not changing the variables that the control IDs were assigned to. When you create the Min_Win GUI, the $Btn_Max control ID is assigned the value of 3. When you delete that GUI going into the Max_Win function, $Btn_Min's control is created with the same Control ID of 3. So even though keep trying to minimize the GUI with the minimize button, all you're doing is calling the wrong function because both variables contain the same control ID now.

What you should be doing, instead of deleting the GUI's, is create both of them, but hide one of them at a time with the buttons, that way the control ID's stay valid.

 

Yea I noticed that when I looked at it with the AutoIt Window Info Tool but wasn't sure how to change it or if it needed to be changed. Thank you for this info. I just need to hit the hay and try again tomorrow. I think I've just been up for too long and the brain needs to do a reset lol. Thank you again for being so patient with me and I will update tomorrow on my progress :).

Link to comment
Share on other sites

  • 6 months later...

Hey BrewManNH! Sorry for the extremely late update. I actually ended up giving up on the project due to frustration I suspect. (Cannot truly remember why). I just read through here again and picked it back up. I finally understand everything you've been telling me since I have been away so now I have a working project due to your diligence! :) Thank you so much for your patience and assistance.

Link to comment
Share on other sites

No worries on the delay.  :)

As long as it's working now for you it's all good.

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