Page 1 of 1

Get content of GUI edit controls using loop

Posted: 25 Nov 2018, 16:32
by Pepineros
Hi all,

I have a GUI with four Edit controls where the user can input data, and a subroutine that checks the contents of every Edit control; if empty, don't do anything, if not empty, append the contents of the edit control and the related text element to clipboard.

For the sake of learning something new and writing more flexible code I would like to do this with a loop instead. Loop four times, using a_index to check each edit one after the other. However: I can't figure out a way to use a_index to identify the vlabel I need.

The code below does what I need it to but relies on four separate if-checks. This is the part I would like to automate using a loop.

Code: Select all

#NoEnv						; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn						; Enable warnings to assist with detecting common errors.
SendMode Input				; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%	; Ensures a consistent starting directory.

gui,assistant:new,resize

gui,add,text,vCID_L,Customer ID
gui,add,text,x156 yp vPID_L,Partner ID
gui,add,edit,xs w108 r1 vCID
gui,add,edit,x156 yp w108 r1 vPID

gui,add,text,xs vORDID_L,Order ID
gui,add,text,x156 yp vCONID_L,Contract ID
gui,add,edit,xs w108 r1 vORDID
gui,add,edit,x156 yp w108 r1 vCONID

gui,add,button,xs w85 r2 gCopy,Copy all

gui,show,,Assistant
return

Copy:
	gui,assistant:submit
	clipboard := "NOTES"
	if(CID != "")
		{
			guicontrolget,CID_L
			clipboard .= "`r`n" CID_L ": " CID
		}
	if(PID != "")
		{
			guicontrolget,PID_L
			clipboard .= "`r`n" PID_L ": " PID
		}
	if(ORDID != "")
		{
			guicontrolget,ORDID_L
			clipboard .= "`r`n" ORDID_L ": " ORDID
		}
	if(CONID != "")
		{
			guicontrolget,CONID_L
			clipboard .= "`r`n" CONID_L ": " CONID
		}
	msgbox, % clipboard
return

GuiClose:
	ExitApp
Thanks in advance for any help!

Re: Get content of GUI edit controls using loop  Topic is solved

Posted: 25 Nov 2018, 19:08
by swagfag
one of two ways, some double-deref fuckery:

Code: Select all

for each, var in ["CID", "PID", "CONID", "ORDID"]
{
	edit := %var%
	labelVar := var "_L"

	GuiControlGet label, , %labelVar%

	if (edit != "")
		clipboard .= "`r`n" label ": " edit
}
or store ur control HWNDs in a collection and later iterate over it:

Code: Select all

#NoEnv						; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn						; Enable warnings to assist with detecting common errors.
SendMode Input				; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%	; Ensures a consistent starting directory.

gui,assistant:new,resize

Controls := []

Controls.Push({"text": guiAdd("Text", "", "Customer ID")})
Controls.Push({"text": guiAdd("Text", "x156 yp", "Partner ID")})
Controls[1, "edit"] := guiAdd("Edit", "xs w108 r1")
Controls[2, "edit"] := guiAdd("Edit", "x156 yp w108 r1")

Controls.Push({"text": guiAdd("Text", "xs", "Order ID")})
Controls.Push({"text": guiAdd("Text", "x156 yp", "Contract ID")})
Controls[3, "edit"] := guiAdd("Edit", "xs w108 r1")
Controls[4, "edit"] := guiAdd("Edit", "x156 yp w108 r1")

gui,add,button,xs w85 r2 gCopy,Copy all

gui,show,,Assistant
return

Copy:
	gui,assistant:submit
	clipboard := "NOTES"

	for each, Ctrl in Controls
	{
		if ((edit := getText(Ctrl.edit)) != "")
			clipboard .= "`r`n" getText(Ctrl.text) ": " edit
	}

	msgbox, % clipboard
return

AssistantGuiClose:
	ExitApp

guiAdd(control, options := "", text := "") {
	Gui Add, % control, % options " +Hwndhwnd", % text
	return hwnd
}

getText(hwnd) {
	GuiControlGet guiCtrlText, , % hwnd
	return guiCtrlText
}
for that ull need some wrapper functions. at this point u might as well consider using ahk v2 instead

Re: Get content of GUI edit controls using loop

Posted: 26 Nov 2018, 01:42
by Pepineros
swagfag wrote:
25 Nov 2018, 19:08
one of two ways, some double-deref fuckery:
Yes, double deref it is. Thanks a mil, I should have looked at a for-loop instead of trying to use a_index!