How to save and load DropDownLists Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
embeddedhustler
Posts: 16
Joined: 07 May 2021, 13:27

How to save and load DropDownLists

07 May 2021, 13:53

Hello guys,

I am working on a script where I need the end-user to use at least 28 DropDownLists. As you might guess, all these will be painful to fill again and again so they must be saved in a setting.ini file and load from there automatically when the script is opened. Every List has a different name, I will just put an example code snippet here for you to help me better. This script is written for a game, every list contains a certain amount of skills from the game. That's why these need to be given precisely and differently so, later on, I can put those skills to good use.

Code: Select all

#SingleInstance Force
#NoEnv
SetWorkingDir %A_ScriptDir%
SetBatchLines -1


Skills = Skill1 | Skill2 | Skill3 | Skill4 | Skill5 | Skill6 | Skill7


;FIRST SKILL BAR

Gui Add, GroupBox, x32 y56 w127 h469, Skill Bar 1

Gui Add, DropDownList, vSkillBar1_SKILL1 x48 y88 w89, 1.Skill ||  %Skills%

Gui Add, DropDownList, vSkillBar1_SKILL2 x48 y120 w89, 2.Skill || %Skills%

Gui Add, DropDownList, vSkillBar1_SKILL3 x48 y151 w89, 3.Skill || %Skills%

Gui Add, DropDownList, vSkillBar1_SKILL4 x48 y182 w89, 4.Skill || %Skills%


;SECOND SKILL BAR

Gui Add, GroupBox, x150 y56 w127 h469, Skill Bar 2

Gui Add, DropDownList, vSkillBar2_SKILL1 x168 y88 w89, 1.Skill || %Skills%

Gui Add, DropDownList, vSkillBar2_SKILL2 X168 y120 w89, 2.Skill || %Skills%

Gui Add, DropDownList, vSkillBar2_SKILL3 x168 y151 w89, 3.Skill || %Skills%

Gui Add, DropDownList, vSkillBar2_SKILL4 x168 y182 w89, 4.Skill || %Skills%



;THIRD SKILL BAR

Gui Add, GroupBox, x268 y56 w127 h469, Skill Bar 3

Gui Add, DropDownList, vSkillBar3_SKILL1 x290 y88 w89, 1.Skill || %Skills%

Gui Add, DropDownList, vSkillBar3_SKILL2 X290 y120 w89, 2.Skill || %Skills%

Gui Add, DropDownList, vSkillBar3_SKILL3 x290 y151 w89, 3.Skill || %Skills%

Gui Add, DropDownList, vSkillBar3_SKILL4 x290 y182 w89, 4.Skill || %Skills%



Gui Show, w782 h622, Window
Return

SaveButton:
Gui, Submit, NoHide

;Some magical things 

Return

GuiEscape:
GuiClose:
ExitApp

I just put 4 skills to keep it short but there will be more... a lot more. I tried a lot of things, I know I have to use IniWrite and IniRead but I am too stupid to handle this. :headwall:
Thanks in advance. :oops:
embeddedhustler
Posts: 16
Joined: 07 May 2021, 13:27

Re: How to save and load DropDownLists

07 May 2021, 14:53

Well.. Thanks for your answer but I've been trying to figure it out since you wrote this comment. I am still kind of lost, would you be kind to elaborate more, please?
User avatar
mikeyww
Posts: 26944
Joined: 09 Sep 2014, 18:38

Re: How to save and load DropDownLists

07 May 2021, 15:10

Sure. You can use IniWrite to save your data into an INI text file. When you run your script, you can use IniRead and GuiControl to populate your GUI as needed. The posted example shows how this is done using an edit field, but you could apply the same approach to a dropdown list.
embeddedhustler
Posts: 16
Joined: 07 May 2021, 13:27

Re: How to save and load DropDownLists

07 May 2021, 15:26

mikeyww wrote:
07 May 2021, 15:10
Sure. You can use IniWrite to save your data into an INI text file. When you run your script, you can use IniRead and GuiControl to populate your GUI as needed. The posted example shows how this is done using an edit field, but you could apply the same approach to a dropdown list.
I see your point now, this is the progress that I made so far;
I created another hypothetical situation for myself to test the things that you said, here is my code:

Code: Select all

Gui, Add, DropDownList, vSelected_Item_From_DropDownList1, pat|cat
Gui, Add, DropDownList, vSelected_Item_From_DropDownList2, pat|cat

IniRead, Read_Item_From_Ini1, setting.ini, Configurations, Selected_Item1
IniRead, Read_Item_From_Ini2, setting.ini, Configurations, Selected_Item2

MsgBox %Read_Item_From_Ini1%
MsgBox %Read_Item_From_Ini2%

Gui, Add, Button,ym,Save

Gui, Show,, Save!
Return

ButtonSave:
Gui, Submit, NoHide

IniWrite, %Selected_Item_From_DropDownList1%, setting.ini, Configurations, Selected_Item1
IniWrite, %Selected_Item_From_DropDownList2%, setting.ini, Configurations, Selected_Item2

Return
I gave these long names to make everything clear for myself, perhaps for other people who might look for a solution to this particular situation.

So now the problem is to put Read_Item_From_Ini and Read_Item_From_Ini2 to the DropDownLists, I know I need to use GuiControl but I am having difficulties doing that.

Maybe you could help me with this?
User avatar
mikeyww
Posts: 26944
Joined: 09 Sep 2014, 18:38

Re: How to save and load DropDownLists  Topic is solved

07 May 2021, 17:13

Code: Select all

ini := StrReplace(A_ScriptFullPath, ".ahk", ".ini"), list := "pat|cat"
Gui, Add, DropDownList, vSelected_Item_From_DropDownList1, %list%
Gui, Add, DropDownList, vSelected_Item_From_DropDownList2, %list%
Gui, Add, Button,, Save
If FileExist(ini) {
 IniRead, Read_Item_From_Ini1, %ini%, Configurations, Selected_Item1
 GuiControl,, Selected_Item_From_DropDownList1, % select(list, Read_Item_From_Ini1)
 IniRead, Read_Item_From_Ini2, %ini%, Configurations, Selected_Item2
 GuiControl,, Selected_Item_From_DropDownList2, % select(list, Read_Item_From_Ini2)
}
Gui, Show, w220, Save
Return

ButtonSave:
Gui, Submit, NoHide
IniWrite, %Selected_Item_From_DropDownList1%, %ini%, Configurations, Selected_Item1
IniWrite, %Selected_Item_From_DropDownList2%, %ini%, Configurations, Selected_Item2
Return

select(list, item){
 Return RegExReplace(list, "(\A|.*\|)(" item ")(\Z|\|)", "|$1$2||")
}
embeddedhustler
Posts: 16
Joined: 07 May 2021, 13:27

Re: How to save and load DropDownLists

08 May 2021, 02:54

mikeyww wrote:
07 May 2021, 17:13

Code: Select all

ini := StrReplace(A_ScriptFullPath, ".ahk", ".ini"), list := "pat|cat"
Gui, Add, DropDownList, vSelected_Item_From_DropDownList1, %list%
Gui, Add, DropDownList, vSelected_Item_From_DropDownList2, %list%
Gui, Add, Button,, Save
If FileExist(ini) {
 IniRead, Read_Item_From_Ini1, %ini%, Configurations, Selected_Item1
 GuiControl,, Selected_Item_From_DropDownList1, % select(list, Read_Item_From_Ini1)
 IniRead, Read_Item_From_Ini2, %ini%, Configurations, Selected_Item2
 GuiControl,, Selected_Item_From_DropDownList2, % select(list, Read_Item_From_Ini2)
}
Gui, Show, w220, Save
Return

ButtonSave:
Gui, Submit, NoHide
IniWrite, %Selected_Item_From_DropDownList1%, %ini%, Configurations, Selected_Item1
IniWrite, %Selected_Item_From_DropDownList2%, %ini%, Configurations, Selected_Item2
Return

select(list, item){
 Return RegExReplace(list, "(\A|.*\|)(" item ")(\Z|\|)", "|$1$2||")
}
I bent this little bit into my project but it worked like a charm. Since I am using Auto-GUI, I do not want to use loops to create these otherwise it would be much easier. If I use loops, it will be hard for me to do changes to my project later on. I do not want to deal with coordinates.

Thanks a lot! :superhappy:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Araphen, Insanibaccha and 226 guests