Generic gLabel for iniwrite function Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
mynameisjohn
Posts: 32
Joined: 27 Mar 2018, 19:38

Generic gLabel for iniwrite function

25 May 2018, 09:32

I'm trying to figure out if it's possible to use a generic gLabel function to write keys with the same name in different sections of a ini.

The code below works, but I'm asking this question specifically to reduce code redundancy since the GUI I'm working on will repeat this pattern about 36 times :shock: and writing a subroutine for each one won't look very pretty.

Code: Select all

#NoEnv
#SingleInstance Force
SetWorkingDir %A_ScriptDir%

IniRead, VarX1, Example.ini, Section1, Var
IniRead, VarX2, Example.ini, Section2, Var
IniRead, VarX3, Example.ini, Section3, Var

Gui Add, GroupBox, x8 y8 w116 h90, Group 1
Gui Add, Text, x16 y53 w32 h23, Var X:
Gui Add, Edit, x16 y68 w45 h21 vVarX1 gVarX1, %VarX1%
Gui Add, UpDown, gVarX1 Range0-999, %VarX1%

Gui Add, GroupBox, x128 y8 w116 h90, Group 2
Gui Add, Text, x136 y53 w32 h23, Var X:
Gui Add, Edit, x136 y68 w45 h21 vVarX2 gVarX2, %VarX2%
Gui Add, UpDown, gVarX2 Range0-999, %VarX2%

Gui Add, GroupBox, x248 y8 w116 h90, Group 3
Gui Add, Text, x256 y53 w32 h23, Var X:
Gui Add, Edit, x256 y68 w45 h21 vVarX3 gVarX3, %VarX3%
Gui Add, UpDown, gVarX3 Range0-999, %VarX3%

Gui Show, w373 h106, Example
Return
	
VarX1:
Gui, Submit, NoHide
IniWrite, %VarX1%, Example.ini, Section1, Var
Return

VarX2:
Gui, Submit, NoHide
IniWrite, %VarX2%, Example.ini, Section2, Var
Return

VarX3:
Gui, Submit, NoHide
IniWrite, %VarX3%, Example.ini, Section3, Var
Return

Code: Select all

[Section1]
Var=7

[Section2]
Var=5

[Section3]
Var=3
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Generic gLabel for iniwrite function  Topic is solved

25 May 2018, 10:57

Try this with a single glabel

Code: Select all

GuiControlGet,Stuff_to_Write,,% A_GuiControl
Tooltip,% A_GuiControl  "`n" Stuff_to_Write
User avatar
mynameisjohn
Posts: 32
Joined: 27 Mar 2018, 19:38

Re: Generic gLabel for iniwrite function

25 May 2018, 14:44

Yes, thank you, the working example now looks like this:

Code: Select all

#NoEnv
#SingleInstance Force
SetWorkingDir %A_ScriptDir%

Loop, 3
{
Counter += 1
IniRead, VarX%Counter%, Example.ini, Section%Counter%, Var
}

Gui Add, GroupBox, x8 y8 w116 h90, Group 1
Gui Add, Text, x16 y53 w32 h23, Var X:
Gui Add, Edit, x16 y68 w45 h21 vSection1 gSingleLabel Number, %VarX1%
Gui Add, UpDown, gSingleLabel Range0-999, %VarX1%

Gui Add, GroupBox, x128 y8 w116 h90, Group 2
Gui Add, Text, x136 y53 w32 h23, Var X:
Gui Add, Edit, x136 y68 w45 h21 vSection2 gSingleLabel Number, %VarX2%
Gui Add, UpDown, gSingleLabel Range0-999, %VarX2%

Gui Add, GroupBox, x248 y8 w116 h90, Group 3
Gui Add, Text, x256 y53 w32 h23, Var X:
Gui Add, Edit, x256 y68 w45 h21 vSection3 gSingleLabel Number, %VarX3%
Gui Add, UpDown, gSingleLabel Range0-999, %VarX3%

Gui Show, w373 h106, Example
Return

SingleLabel:
GuiControlGet,Stuff_to_Write,,% A_GuiControl
IniWrite, %Stuff_to_Write%, Example.ini, % A_GuiControl, Var
Return
But when I use UpDown controls, it creates a blank section and variable with Var key name in the end of the ini file. This don't cause any erratic behavior, I'd just like to figure out why this happens and if there is a way to avoid it. The end of the ini file looks like this after using UpDown:

Code: Select all

[Section1]
Var=7

[Section2]
Var=5

[Section3]
Var=3
[]
Var=
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Generic gLabel for iniwrite function

25 May 2018, 15:22

You can try making the vars for each section set up like this and then get rid of the last digits.

So section1 vars could be section11 - section12 , etc
section2 vars could be section21 - section22 , etc

Code: Select all

#SingleInstance,Force


Gui ,Add, Edit,x10 y10 w200 r1 vSection1233 gSingleLabel Number, %VarX1%
Gui, Add, UpDown,  vSection134  Range0-999, %VarX1%
gui,show,w300 h200
return

GuiClose:
	ExitApp

SingleLabel:
	GuiControlGet,Stuff_to_Write,,% A_GuiControl
	StringLeft,section,A_GuiControl,8
	tooltip,% Stuff_to_Write "`n" A_GuiControl "`n" Section
	;~ IniWrite, %Stuff_to_Write%, Example.ini, % Section, Var
	return
Last edited by Hellbent on 25 May 2018, 15:43, edited 2 times in total.
burque505
Posts: 1734
Joined: 22 Jan 2017, 19:37

Re: Generic gLabel for iniwrite function

25 May 2018, 15:34

EDIT: Sorry, Hellbent, I missed your post.
Hi, try this:

Code: Select all

#NoEnv
#SingleInstance Force
SetWorkingDir %A_ScriptDir%

Loop, 3
{
Counter += 1
IniRead, VarX%Counter%, Example.ini, Section%Counter%, Var
}

Gui Add, GroupBox, x8 y8 w116 h90, Group 1
Gui Add, Text, x16 y53 w32 h23, Var X:
Gui Add, Edit, x16 y68 w45 h21 vSection1 gSingleLabel Number, %VarX1%
Gui Add, UpDown, Range0-999, %VarX1%

Gui Add, GroupBox, x128 y8 w116 h90, Group 2
Gui Add, Text, x136 y53 w32 h23, Var X:
Gui Add, Edit, x136 y68 w45 h21 vSection2 gSingleLabel Number, %VarX2%
Gui Add, UpDown, Range0-999, %VarX2%

Gui Add, GroupBox, x248 y8 w116 h90, Group 3
Gui Add, Text, x256 y53 w32 h23, Var X:
Gui Add, Edit, x256 y68 w45 h21 vSection3 gSingleLabel Number, %VarX3%
Gui Add, UpDown, Range0-999, %VarX3%

Gui Show, w373 h106, Example
Return

SingleLabel:
GuiControlGet,Stuff_to_Write,,% A_GuiControl
IniWrite, %Stuff_to_Write%, Example.ini, % A_GuiControl, Var
Return

Escape::ExitApp
Regards,
burque505
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Generic gLabel for iniwrite function

25 May 2018, 15:40

burque505 wrote:EDIT: Sorry, Hellbent, I missed your post.
It's all good, I posted it backwards in mine anyway, having the var moved to the updown.

You have it the correct way. My edit above has me trying to fix my first error by making it even more complicated.

On the other hand, my edit does show a trick of sorts that can be useful in other instances.
User avatar
mynameisjohn
Posts: 32
Joined: 27 Mar 2018, 19:38

Re: Generic gLabel for iniwrite function

25 May 2018, 17:16

Yes, removing gLabel from UpDown did the trick, thank you both Hellbent and burque505, your help was much appreciated.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Joey5 and 208 guests