help assigning dynamically named variable to gui.add()

Post AHK_H specific scripts & libraries and discuss the usage and development of HotKeyIt's fork/branch
chipy
Posts: 3
Joined: 29 Nov 2017, 23:43
GitHub: iamchipy

help assigning dynamically named variable to gui.add()

01 Dec 2017, 15:36

hey everyone, this is a rather niche issue so i might just not be searching for help correctly but maybe you guys can direct me.

I'm trying to build a GUI that can be fed a dynamic number of inputs(read from a file) and have it display them for editing and then save back to file.
I'm not sure this is best but i'm trying to adapt my code from AHK_L so it's kinda bumpy so i've made this simplified version to try and help to diagnose what I'm doing wrong.
I think my primary issue is that i do not know how to assign a dynamic variable to a GUI control, which i need to do so that the gui can handle 10 20 or 50 items with connected variables. However, i do not know how to save them with the AHK v2 output system which seems to use a OOP style that i know very little about. (it's possible i'm just looking at the wrong version of AHK's manual and it's all there but maybe not and i could really use help to save me time from someone that knows.)

here is my code (i'm using "2.0-alpha")

"cfg.ini"

Code: Select all

"cfg.ini"
[Info]
PartCount=5
[1]
Name=AutoCompile
File=CMS_Part_Recompiler.ahk
[2]
Name=Attributes
File=CMS_Part_Attributes.ahk
[3]
Name=Globals
File=CMS_Part_Globals.ahk
[4]
Name=Setup
File=CMS_Func_Setup.ahk
[5]
script

Code: Select all

#SingleInstance force
global PartCount
global names := Object(), files := Object()
MsgBox(A_AhkVersion)
GoGUI:
	gui := GuiCreate() 												;create gui-obj
	PartCount := IniRead("cfg.ini", "Info", "PartCount")			;reading ini file to get the number of entries to make for this gui

	loop partcount 													;loop for each part line of the gui
	{
		names.Push(iniread("cfg.ini", A_Index,"Name"))				;reads data from file for gui population
		files.Push(iniread("cfg.ini", A_Index,"File"))				;reads data from file for gui population
;;;;;;;;;;;;;;;;;;;;;;; THIS IS THE SECTION THAT I THINK NEEDS HELP ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		gui.add( "text", "xm" , A_Index)
		gui.add( "edit", "x+m vnames[" . A_Index . "]" , names[A_Index])
		gui.add( "edit", "x+m vfiles[" . A_Index . "]" , files[A_Index])
;;;;;;;;;;;;;;;;;;;;;;; THIS IS THE SECTION THAT I THINK NEEDS HELP ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	}

	SubSave := Func("BtnSave").bind(gui)							;binds 'btnSave' function with the gui-obj for latter form submission
	gui.add( "Button","xm" ,"Save").OnEvent("Click",SubSave)		;adds button and links 'btnSave' function to it when clicked
	gui.show()														;shows the gui 
Return


BtnSave(gui){
	saved := gui.submit()											;should store variables from the gui all inside "saved" object as i understand
	loop PartCount 													;loops for each entry in the gui for to save back to file
	{
		txt := "debug output:`r`n"									;sets up a debug txt for me to check what i'm attempting to "save" to file
		txt .= A_Index . " " . saved.files[A_Index] . "`r`n"		
		txt .= A_Index . " " . saved.names[A_Index] . "`r`n"
		MsgBox(txt)													;msgbox with debug text
		TransName := saved.names[A_Index]							;bouces the object info into another variable 
		TransFile := saved.files[A_Index]							;(this was my work around for "contains illegal characters")
		IniWrite(TransName, "cfg.ini", A_Index, "Name")				;writes data to file
		IniWrite(TransFile, "cfg.ini", A_Index, "File")				;writes data to file
	}
	gosub GoGUI														;reloads the gui
}
HotKeyIt
Posts: 1697
Joined: 29 Sep 2013, 18:35
Contact:

Re: help assigning dynamically named variable to gui.add()

01 Dec 2017, 16:55

Try this:

Code: Select all

/*
"cfg.ini"
[Info]
PartCount=5
[1]
Name=AutoComplete
File=CMS_Part_Recompiler.ahk
[2]
Name=Attributes
File=CMS_Part_Attributes.ahk
[3]
Name=Globals
File=CMS_Part_Globals.ahk
[4]
Name=Setup
File=CMS_Func_Setup.ahk
[5]
*/
#SingleInstance force
global PartCount

GoGUI:
	gui := GuiCreate() 												;create gui-obj
	PartCount := IniRead(A_ScriptFullPath, "Info", "PartCount")			;reading ini file to get the number of entries to make for this gui
global names := [], files := []
	loop partcount 													;loop for each part line of the gui
	{
		names.Push(iniread(A_ScriptFullPath, A_Index,"Name"))				;reads data from file for gui population
		files.Push(iniread(A_ScriptFullPath, A_Index,"File"))				;reads data from file for gui population
;;;;;;;;;;;;;;;;;;;;;;; THIS IS THE SECTION THAT I THINK NEEDS HELP ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		gui.add( "text", "xm" , A_Index)
		gui.add( "edit", "x+m vnames" A_Index, names[A_Index])
		gui.add( "edit", "x+m vfiles" A_Index, files[A_Index])
;;;;;;;;;;;;;;;;;;;;;;; THIS IS THE SECTION THAT I THINK NEEDS HELP ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	}

	SubSave := Func("BtnSave").bind(gui)							;binds 'btnSave' function with the gui-obj for latter form submission
	gui.add( "Button","xm" ,"Save").OnEvent("Click",SubSave)		;adds button and links 'btnSave' function to it when clicked
	gui.show()														;shows the gui 
Return


BtnSave(gui){
	saved := gui.submit()											;should store variables from the gui all inside "saved" object as i understand
	loop PartCount 													;loops for each entry in the gui for to save back to file
	{
		txt := "debug output:`r`n"									;sets up a debug txt for me to check what i'm attempting to "save" to file
		txt .= A_Index . " " . saved["files" A_Index] . "`r`n"		
		txt .= A_Index . " " . saved["names" A_Index] . "`r`n"
		MsgBox(txt)													;msgbox with debug text
		TransName := saved["names" A_Index]							;bouces the object info into another variable 
		TransFile := saved["files" A_Index]							;(this was my work around for "contains illegal characters")
		IniWrite(TransName, A_ScriptFullPath, A_Index, "Name")				;writes data to file
		IniWrite(TransFile, A_ScriptFullPath, A_Index, "File")				;writes data to file
	}
    gui.Destroy()
	gosub GoGUI														;reloads the gui
}
chipy
Posts: 3
Joined: 29 Nov 2017, 23:43
GitHub: iamchipy

Re: help assigning dynamically named variable to gui.add()

01 Dec 2017, 17:11

wow thanks! it does seem to work, i'll be taking time tonight to decode the changes to make sure i understand them. thanks for the prompt response bro!

Return to “AutoHotkey_H”

Who is online

Users browsing this forum: No registered users and 1 guest