AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

help! Saving and reloading control variables

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
JohnL



Joined: 04 Oct 2005
Posts: 35

PostPosted: Mon Dec 19, 2005 5:45 am    Post subject: help! Saving and reloading control variables Reply with quote

I save my GUI's controls for the user to an ini file upon exit, and reselect them on load. I find that sometimes, when I reload the program (I think exiting works ok) some controls are not preselected and the ini file does not reflect what should have saved the last exit.

My first question is... is there a better way to save and load controls?
Second.. why is it missing save's occasionally? do I need more delays?
Third.. I think it may have something to do with when the program is reloaded. An example is onexit, it still saves controls, and if the controls aren't loaded (say you pause before the UI is even built) then it will write blank entries.

Thanks for any help...
Back to top
View user's profile Send private message
toralf



Joined: 31 Jan 2005
Posts: 3842
Location: Bremen, Germany

PostPosted: Mon Dec 19, 2005 9:43 am    Post subject: Reply with quote

I use ini files to store the settings in a GUI. It always worked for me and I have complex GUIs, so it must have to do with your code. Please post a small part that demos your problem. Delays are normally not necessary.
_________________
Ciao
toralf
Back to top
View user's profile Send private message Send e-mail Visit poster's website
JohnL



Joined: 04 Oct 2005
Posts: 35

PostPosted: Tue Dec 20, 2005 12:40 am    Post subject: Reply with quote

Apparently this has been happening more often then I thought, especially in check boxes.. it also happens on normal exits, not just reloads. So I bet this is an error on my part.

I use ini files also. In the main ini file is a section called Saved.

After the GUI's controls have been created, it calls LoadSaved, which is something like:

Code:

LoadSaved:

   IniRead, History, file.ini, Saved, History
   IniRead, Option1, file.ini, Saved, Option1
   IniRead, Option2, file.ini, Saved, Option2
   IniRead, Option3, file.ini, Saved, Option3
   IniRead, Option4, file.ini, Saved, Option4
   IniRead, Option5, file.ini, Saved, Option5
   IniRead, Option6, file.ini, Saved, Option6
   IniRead, Option7, file.ini, Saved, Option7
   IniRead, Option8, file.ini, Saved, Option8
   IniRead, Option9, file.ini, Saved, Option9
   IniRead, FastLoad, file.ini, Saved, FastLoad   

   GuiControl, Choose, History, |%History%
   GuiControl, Choose, Option1, |%Option1%
   GuiControl, Choose, Option2, |%Option2%
   GuiControl, Choose, Option3, |%Option3%
   GuiControl, Choose, Option4, |%Option4%
   GuiControl, Choose, Option5, |%Option5%
   GuiControl, Choose, Option6, |%Option6%
   GuiControl, Choose, Option7, |%Option7%
   GuiControl, Choose, Option8, |%Option8%
   GuiControl, Choose, Option9, |%Option9%
   GuiControl, , FastLoad, %FastLoad%

Return



OnExit, GuiClose, etc, goes like this:

Code:

Save:
   Gui, Submit, NoHide
   IniWrite, %History%, file.ini, Saved, History
   IniWrite, %Option1%, file.ini, Saved, Option1
   IniWrite, %Option2%, file.ini, Saved, Option2
   IniWrite, %Option3%, file.ini, Saved, Option3
   IniWrite, %Option4%, file.ini, Saved, Option4
   IniWrite, %Option5%, file.ini, Saved, Option5
   IniWrite, %Option6%, file.ini, Saved, Option6
   IniWrite, %Option7%, file.ini, Saved, Option7
   IniWrite, %Option8%, file.ini, Saved, Option8
   IniWrite, %Option9%, file.ini, Saved, Option9
   IniWrite, %FastLoad%, file.ini, Saved, FastLoad
   
ExitApp
Back to top
View user's profile Send private message
Titan



Joined: 11 Aug 2004
Posts: 5068
Location: imaginationland

PostPosted: Tue Dec 20, 2005 1:18 am    Post subject: Reply with quote

JohnL wrote:
After the GUI's controls have been created, it calls LoadSaved

Try to call this label before you create the GUI. That way you can specify checked/unchecked values in the control's option at run time. Example:
Code:
IniRead, CheckedValue, settings.ini, GuiPreferences, ExampleCheckbox, 0
Gui, Add, Checkbox, Checked%CheckedValue%, Control
Back to top
View user's profile Send private message Visit poster's website
JohnL



Joined: 04 Oct 2005
Posts: 35

PostPosted: Tue Dec 20, 2005 1:36 am    Post subject: Reply with quote

Great, I put that in, and I can see how it might improve the GUI.

I'm not sure how I could use this method to preselect a DDL.. maybe some help there?

It also still doesn't suggest as to why the save procedure is failing. It's as if it thinks there is nothing in the controls and thus, saves nothing. Mad I can tell its doing this because when a control doesn't reload like it shold, it shows in the ini file that there is nothing there.. and the only time it writes to the file is when it quits.
Back to top
View user's profile Send private message
Titan



Joined: 11 Aug 2004
Posts: 5068
Location: imaginationland

PostPosted: Tue Dec 20, 2005 1:41 am    Post subject: Reply with quote

JohnL wrote:
I'm not sure how I could use this method to preselect a DDL.. maybe some help there?

AutoHotkey Docs wrote:
To have one of the items pre-selected when the window first appears, include two pipe characters after it.


There are numerous reasons for why it didn't work before. Without inspecting all of the code, I can't really pinpoint the problem. I doubt that a bug in AutoHotkey was causing the situation, most likely a typo somwhere in your script or an invalid command? Confused
Back to top
View user's profile Send private message Visit poster's website
ranomore



Joined: 06 Nov 2004
Posts: 178
Location: Salt Lake City, UT

PostPosted: Tue Dec 20, 2005 1:41 am    Post subject: Reply with quote

I think it would be helpful also if we could see how you are adding these controls in the GUI. Anyway, here is something similar to what I use for working with an ini file

Code:

guiTitle := "Save Config"
Gui,Add,Edit,voption1 w150,% ReadINI("option1","1")
Gui,Add,Edit,voption2 w150,% ReadINI("option2","2")
Gui,Add,Edit,voption3 w150,% ReadINI("option3","3")
Gui,Add,Edit,voption4 w150,% ReadINI("option4","4")
Gui,Add,button,,Save Config
Gui,Add,button,,Read Config
Gui,Show,,%guiTitle%
return


buttonSaveConfig:
Gui,+disabled
saveConfig(guiTitle)
gui,-disabled
return

buttonReadConfig:
Gui,+disabled
readConfig(guiTitle)
gui,-disabled
return

;this one loops through the controls in the window,
;looking for edit controls
SaveConfig(window)
{
   Global cx_defaultWebsite
   HWND := WinExist(window)
   WinGet,ControlList,ControlList,ahk_id %HWND%
   Loop,Parse,controlList,`n
   {
      If inStr(a_LoopField,"Edit")
      {
         ControlGetText,char,%a_loopField%,ahk_id %HWND%
         StringReplace,val,a_loopField,edit
         saveINI(GetVarName(val),char)
      }
   }
}

;;use this one to load changed configurations
ReadConfig(window)
{
   Global cx_defaultWebsite
   HWND := WinExist(window)
   WinGet,ControlList,ControlList,ahk_id %HWND%
   Loop,Parse,controlList,`n
   {
      If inStr(a_LoopField,"Edit")
      {
         StringReplace,val,a_loopField,edit
         GuiControl,,% getVarName(val),% ReadINI(GetVarName(val),"")
      }
   }
}

getVarName(val)
{
   If val = 1
      rtn := "option1"
   Else if val = 2
      rtn := "option2"
   Else if val = 3
      rtn := "option3"
   Else if val = 4
      rtn := "option4"
   return rtn
}

saveINI(key,val)
{
   configFile := slash(a_scriptdir) "file.ini"
   SplitPath,a_scriptname,,,,OutNameNoExt
   IniWrite,%val%,%configFile%,%OutNameNoExt%,%key%
}

ReadINI(key,defaultVal = 0)
{
   configFile := slash(a_scriptdir) "file.ini"
   SplitPath,a_scriptname,,,,OutNameNoExt
   IniRead,val,%configFile%,%OutNameNoExt%,%key%
   IF (Val = "Error")
      val := defaultVal
   Else if inStr(val,"`n")
      StringReplace,val,val,`n,`r`n
   return %val%
}

slash(dir)
{
   StringRight, slash,dir, 1
   IfNotInString, slash, \
      dir = %dir%\
   Return %dir%
}


EDIT:
Quote:
AutoHotkey Docs wrote:
To have one of the items pre-selected when the window first appears, include two pipe characters after it.


There is also a distinction between Choose and ChooseString. Are you saving the position of the DDL, or the Contents of it?
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
JohnL



Joined: 04 Oct 2005
Posts: 35

PostPosted: Tue Dec 20, 2005 2:09 am    Post subject: Reply with quote

Titan: I realized that might be a possible way, but that creates a new entry for the DDL, duplicating what already exists.. that's why I used GuiControl, Choose instead. I could use this method but.. this particular part of the code (loading) doesn't seem to be the culprit.

ranomore: Thanks for your code, although its a bit over my head, I'm going to take a look at it and hopefully be able to take some advantage of it.

The controls are added in no particular manner:
Gui, Add, DropDownList, vOption1 gSubmit w200, |First Option: %option1%|Second Option: %option2%|etc|etc|

And wow, I just realized I've been using Choose this entire time. In the docs, I noticed that if Choose isn't an integer, it will use ChooseString anyways though. I did some testing and it SEEMS like maybe this was the problem. Not sure how but.. this could have been it. I'll have to test it more.
Back to top
View user's profile Send private message
ranomore



Joined: 06 Nov 2004
Posts: 178
Location: Salt Lake City, UT

PostPosted: Tue Dec 20, 2005 5:49 pm    Post subject: Reply with quote

Quote:
And wow, I just realized I've been using Choose this entire time. In the docs, I noticed that if Choose isn't an integer, it will use ChooseString anyways though. I did some testing and it SEEMS like maybe this was the problem. Not sure how but.. this could have been it. I'll have to test it more.


If you always want to save the position of the selected drop down item (instead of the text), you need to use the AltSubmit (http://www.autohotkey.com/docs/commands/GuiControls.htm) Property:

Gui, Add, DropDownList, vOption1 AltSubmit gSubmit w200, |First Option: %option1%|Second Option: %option2%|etc|etc|
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group