Destroy GUI after passing variable to function using GuiControl Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Vlad21
Posts: 8
Joined: 04 Jan 2023, 21:26

Destroy GUI after passing variable to function using GuiControl

Post by Vlad21 » 30 Jan 2023, 14:32

I'm using a GUI button to activate a function. Since I'm still on version 1 the only way to do this while passing the variables is by binding the function.
I got this working but what I can't figure out is how to destroy the GUI after the button has been clicked.
Here is my code:

Code: Select all


	BoundLoginFunc := func("Login_Operation").Bind(Login,OperationID)

	Gui Add, Button, hWndhBtnConfirm2 vConfirm x156 y129 w205 h64, CONFIRM

	GuiControl, +g, Confirm, %BoundLoginFunc%
	
I tired adding a g-Label to the Confirm button with Gui Destroy in it but that didn't work.
The way I was able to get the GUI to destroy was by adding a GUI destroy to the end of the function Login_Operation().
Even though this works I don't feel like it's the correct way of doing it. Does anyone know of a different method?

User avatar
mikeyww
Posts: 26849
Joined: 09 Sep 2014, 18:38

Re: Destroy GUI after passing variable to function using GuiControl  Topic is solved

Post by mikeyww » 30 Jan 2023, 14:52

It is correct the way you have described it. To act when the button is activated, you would put the action into the subroutine or function that is called upon the activation-- which is what you have described.

Vlad21
Posts: 8
Joined: 04 Jan 2023, 21:26

Re: Destroy GUI after passing variable to function using GuiControl

Post by Vlad21 » 30 Jan 2023, 15:01

Thank you for the response.
I seemed like a round about way of getting it to work. For whatever reason I felt like I should be able to destroy the Gui within the Gui part of the script.

User avatar
mikeyww
Posts: 26849
Joined: 09 Sep 2014, 18:38

Re: Destroy GUI after passing variable to function using GuiControl

Post by mikeyww » 30 Jan 2023, 16:41

Quite a few AHK coders new to the GUI express challenges in understanding the flow, but this is how it works, as the Gui setup itself does not wait or pause for something to happen-- unlike how an InputBox works. Thus, the actions are tied to the calls that are triggered by events such as a button activation.

Vlad21
Posts: 8
Joined: 04 Jan 2023, 21:26

Re: Destroy GUI after passing variable to function using GuiControl

Post by Vlad21 » 30 Jan 2023, 17:57

Yes I'm definitely having lots of trouble with GUIs and understanding exactly how they flow.
I have another question if you don't mind answering. This also pertains to the flow.
I'm trying to active a Function with the press of button which worked so far. Next what I want to do is get the user to input some variables into an Edit then press the button and pass those variables onto the function.
The problem I'm having is since I'm passing variables onto the function I need to use the bound function along with GuiControl. In this case the variables stored in the Edit are not submitted so nothing is passed on.
I tried using GuiControlGet instead, but it seems this doesn't wait for me to enter the variables. Instead as soon as the GUI is loaded it grabs whatever values are preset in the Edit and then passes those on.
I tried putting a loop around the GuiControl hoping it would constantly loop through and update the variables but the script gets stuck here since there's no break.
Here's my code:

Code: Select all


		Gui Add, Edit, vEnteredQTY x158 y166 w120 h32 +Center
		Gui Add, Edit, vEnteredScrapQTY x594 y166 w120 h32 -VScroll
		Gui Add, DropDownList, vEnteredOpComplete x504 y238 w70, NO||YES
		Gui Add, Button, vLogout x217 y320 w165 h46, LOGOUT

		GuiControlGet, EnteredOpComplete
		GuiControlGet, EnteredQTY
		GuiControlGet, EnteredScrapQTY
		
		BoundLogoutFunc := func("Logout_Operation").Bind(EnteredOpComplete,EnteredQTY,EnteredScrapQTY)
		GuiControl, +g, Logout, %BoundLogoutFunc%
		
At this point I'm out of ideas. I need to somehow submit the GUI or get the variables from the Edit. But I can't use the g-label since I'm passing variables to a bound function with GuiControl.

User avatar
mikeyww
Posts: 26849
Joined: 09 Sep 2014, 18:38

Re: Destroy GUI after passing variable to function using GuiControl

Post by mikeyww » 30 Jan 2023, 18:18

One option for submitting the GUI is to use Submit. :D It's easier than what you have written.
Saves the contents of each control to its associated variable (if any) and hides the window unless the NoHide option is present.

Code: Select all

#Requires AutoHotkey v1.1.33
Gui Add, Edit, vtxt
Gui Add, Button,, Submit
Gui Show
Return

ButtonSubmit:
Gui Submit
MsgBox % txt
Return

User avatar
boiler
Posts: 16902
Joined: 21 Dec 2014, 02:44

Re: Destroy GUI after passing variable to function using GuiControl

Post by boiler » 30 Jan 2023, 18:29

Vlad21 wrote: Yes I'm definitely having lots of trouble with GUIs and understanding exactly how they flow.
I tried using GuiControlGet instead, but it seems this doesn't wait for me to enter the variables. Instead as soon as the GUI is loaded it grabs whatever values are preset in the Edit and then passes those on.
That's because you're executing those lines right after the creation of the hotkey. When you structure it the proper way like mikeyww showed, you don't execute any code after the GUI is created until the event is triggered. It's analogous to this simple hotkey script:

Code: Select all

Name := "Doyle"
return ; end of auto-execute section

Tab::MsgBox, % "Hello, " Name "!"
The only part of the script that goes before the initial return (i.e., in the auto-execute section) is what you want executed immediately upon the script starting. The hotkey doesn't execute until the Tab key is pressed. In a GUI script, you put the part that gets the values from the GUI controls and acts on them in a subroutine or function that is triggered by a GUI button click or some other defined event.

just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Destroy GUI after passing variable to function using GuiControl

Post by just me » 31 Jan 2023, 05:14

Vlad21 wrote:
30 Jan 2023, 17:57
Next what I want to do is get the user to input some variables into an Edit then press the button and pass those variables onto the function.
The problem I'm having is since I'm passing variables onto the function I need to use the bound function along with GuiControl.
You don't need to pass values of controls contained in the same GUI as the button to the button's function. You can retrieve the values within the function either with Gui, Submit, NoHide or GuiControlGet, ControlVariable.

Code: Select all

#NoEnv
Gui, Margin, 100, 10
Gui, Add, Edit, w200 vInput, Some input!
Gui, Add, Button, w200 gDone, All done!
Gui, Show, , Example
Return
Done(CtrlHwnd, GuiEvent, EventInfo, ErrLevel := "")  {
   GuiControlGet, Input
   MsgBox, 0, Your input is:,  %Input%
   MsgBox, 0, Destroy, Going to destroy the Gui!
   Gui, Destroy
}
Esc::ExitApp

Vlad21
Posts: 8
Joined: 04 Jan 2023, 21:26

Re: Destroy GUI after passing variable to function using GuiControl

Post by Vlad21 » 31 Jan 2023, 09:12

In an attempt to simplify my code to try and focus on the problem I'm having I over simplified it.
I have a function called Logout_GUI(Login,OperationID,RunType,LogoutType), which contains multiple GUIs inside it.
4 variables are passed to this function, where one of the variables, LogoutType determines which GUI is called.
The GUI is then shown and asks for an additional 3 variables. I now need to pass those 3 variables along with the remaining 3 variables that were passed to the Logout_GUI function.
In essences I'm getting 3 variables outside of the GUI then requesting another 3 variables with the GUI and need to pass all 6 along to another function.

Here's the code if I use the GUI Submit

Code: Select all

Logout_GUI("1234","388191","Run","Operator")

Logout_GUI(Login,OperationID,RunType,LogoutType)
{
	Global EnteredQTY
	Global EnteredScrapQTY
	Global EnteredOpComplete
	Global Logout
	
	If (LogoutType = "Operator")
	{
		Gui +hWndhMainWnd
		Gui Font, s20 Bold
		Gui Add, Text, x130 y46 w620 h45 +0x200 +Center, Logout of Operation
		Gui Font
		Gui Font, s15 Bold
		Gui Add, Text, x76 y118 w283 h23 +0x200 +Center, Enter QTY Completed
		Gui Font
		Gui Font, s15 Bold
		Gui Add, Edit, vEnteredQTY x158 y166 w120 h32 +Center
		Gui Font
		Gui Font, s15 Bold
		Gui Add, Button, hWndhBtnConfirm gLogout x217 y320 w165 h46, LOGOUT
		Gui Font
		Gui Font, s15 Bold
		Gui Add, Button, hWndhBtnCancel2 x479 y320 w165 h46, CANCEL
		Gui Font
		Gui Font, s15 Bold
		Gui Add, Text, x511 y120 w283 h23 +0x200 +Center, Enter Scrap QTY
		Gui Add, Edit, vEnteredScrapQTY x594 y166 w120 h32 -VScroll
		Gui Font
		Gui Font, s15 Bold
		Gui Add, Text, x258 y242 w213 h23 +0x200 +Center, Operation Complete?
		Gui Font
		Gui Font, s15 Bold
		Gui Add, DropDownList, vEnteredOpComplete x504 y238 w70, NO||YES
		Gui Font
		
		Gui Show, x378 y338 w880 h417, Window 
		Return
		
		Logout:
			GUI Submit
			msgbox % EnteredQTY "`n" EnteredScrapQTY "`n"  EnteredOpComplete
			msgbox % Login "`n" OperationID "`n" RunType
		
		GuiLogoutEscape:
		GuiLogoutClose:
		ExitApp
	}
}
When using the GUI submit the 3 variables from the GUI are passed on but the 3 variables from outside the GUI (i.e Login, OperationID, RunType) return a blank.
To get around this I need to bind the function. So I changes the g-label gLogout to vlogout and get the following:

Code: Select all

Logout_GUI("1234","388191","Run","Operator")

Logout_GUI(Login,OperationID,RunType,LogoutType)
{
	Global EnteredQTY
	Global EnteredScrapQTY
	Global EnteredOpComplete
	Global Logout
	
	If (LogoutType = "Operator")
	{
		
		
		Gui +hWndhMainWnd
		Gui Font, s20 Bold
		Gui Add, Text, x130 y46 w620 h45 +0x200 +Center, Logout of Operation
		Gui Font
		Gui Font, s15 Bold
		Gui Add, Text, x76 y118 w283 h23 +0x200 +Center, Enter QTY Completed
		Gui Font
		Gui Font, s15 Bold
		Gui Add, Edit, vEnteredQTY x158 y166 w120 h32 +Center
		Gui Font
		Gui Font, s15 Bold
		Gui Add, Button, hWndhBtnConfirm vLogout x217 y320 w165 h46, LOGOUT
		Gui Font
		Gui Font, s15 Bold
		Gui Add, Button, hWndhBtnCancel2 x479 y320 w165 h46, CANCEL
		Gui Font
		Gui Font, s15 Bold
		Gui Add, Text, x511 y120 w283 h23 +0x200 +Center, Enter Scrap QTY
		Gui Add, Edit, vEnteredScrapQTY x594 y166 w120 h32 -VScroll
		Gui Font
		Gui Font, s15 Bold
		Gui Add, Text, x258 y242 w213 h23 +0x200 +Center, Operation Complete?
		Gui Font
		Gui Font, s15 Bold
		Gui Add, DropDownList, vEnteredOpComplete x504 y238 w70, NO||YES
		Gui Font
		
		Gui Show, x378 y338 w880 h417, Window 
		Return
		
		GuiControlGet, EnteredOpComplete
		GuiControlGet, EnteredQTY
		GuiControlGet, EnteredScrapQTY
		
		BoundLogoutFunc := func("Logout_Operation").Bind(Login,OperationID,RunType,EnteredOpComplete,EnteredQTY,EnteredScrapQTY)
		GuiControl, +g, Logout, %BoundLogoutFunc%
		
		GuiLogoutEscape:
		GuiLogoutClose:
		ExitApp
	}
	
}
Now the 3 variables outside the GUI are passed along but this code is executed as soon as the GUI loads not giving me time to enter the variables in the GUI.
I can't put the bound function inside a submit since those variables are lost before it reaches that point.
I tried doing what just me suggest and placing the bound function inside a function. However I get an error saying you can't place a function inside a function.

I'm trying to wrap my brain around this. The only thing I can think of is if I bind the function when the GUI is created without the 3 variables that the GUI asks for. Then I somehow submit that bound function along with the variables from the GUI.
I'm not really sure how I would do this since the act executing the function is done by:
GuiControl, +g, Logout, %BoundLogoutFunc%
At the same time submitting the GUI is done by

Code: Select all

	Logout:
	GUI Submit
Both of these involve clicking the same button. I guess I would have to use a different control to bind the function.
Is what I'm trying to do even possible?

Vlad21
Posts: 8
Joined: 04 Jan 2023, 21:26

Re: Destroy GUI after passing variable to function using GuiControl

Post by Vlad21 » 31 Jan 2023, 12:16

I found a work around. I created 3 other edit boxes.
When the GUI is created the value of these edit boxes is set to the variables I'm trying to pass from outside of the GUI.
Then when the GUI is submitted these values get passed.
These Edit boxes are not visible on the GUI and are just used to pass these variables after the submit to the next function.
It might not be the proper way of doing it but was the only solution I could come up with.

Post Reply

Return to “Ask for Help (v1)”