Learning IniWrite & IniRead Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
LAPIII
Posts: 671
Joined: 01 Aug 2021, 06:01

Learning IniWrite & IniRead

25 Feb 2022, 11:51

  1. Where istrhe ini file saved to:

    Code: Select all

    Gui, Show, x131 y91 h296 w300, New GUI Window
    Gui, Add, Edit, x46 y50 w100 h30 vMyEdit1, Edit1
    
    Gui, Add, Button, x46 y180 w100 h40 , Save
    Gui, Add, Button, x166 y180 w100 h40 , Load
    Return
    
    ButtonSave:
    Gui, Submit, NoHide
    IniWrite, %MyEdit1%, settings.ini, history, edit1
    Return
    
    ButtonLoad:
    IniRead, MyEdit1, settings.ini, history, edit1
    
    GuiControl, , MyEdit1, %MyEdit1%
    Return
    
    GuiClose:
    ExitApp
  2. Can I make the GUI automatically save and load the ini file?
Last edited by LAPIII on 13 Mar 2022, 17:59, edited 1 time in total.
amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Learning IniWrite & IniRead

25 Feb 2022, 12:11

1.
Filename
The name of the .ini file, which is assumed to be in %A_WorkingDir% if an absolute path isn't specified.
2. What do you mean saying "automatically"?
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.
LAPIII
Posts: 671
Joined: 01 Aug 2021, 06:01

Re: Learning IniWrite & IniRead

25 Feb 2022, 12:15

Notepad++ automatically saves and loads such a file so I can write in the unsaved notes there and they'll always load when opening the app.
amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Learning IniWrite & IniRead

25 Feb 2022, 13:25

I think it will look something like this:

Code: Select all

Gui, Show, x131 y91 h296 w300, New GUI Window
Gui, Add, Edit, x46 y50 w100 h30 vMyEdit1,
Gui, Add, Button, x46 y180 w100 h40 , Save
Gui, Add, Button, x166 y180 w100 h40 , Load
gosub, ButtonLoad
OnExit, ExitSub
Return

ButtonSave:
Gui, Submit, NoHide
IniWrite, %MyEdit1%, settings.ini, history, edit1
Return

ButtonLoad:
IniRead, MyEdit1, settings.ini, history, edit1
GuiControl, , MyEdit1, %MyEdit1%
Return

GuiClose:
ExitApp

ExitSub:
gosub, ButtonSave
ExitApp
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.
LAPIII
Posts: 671
Joined: 01 Aug 2021, 06:01

Re: Learning IniWrite & IniRead

25 Feb 2022, 13:39

:P Terrific, now can you get rid of the buttons please?
amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Learning IniWrite & IniRead

25 Feb 2022, 13:44

Try to do that by your own. If you'll fail, post here your best attempt and we'll fix it.
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.
LAPIII
Posts: 671
Joined: 01 Aug 2021, 06:01

Re: Learning IniWrite & IniRead

25 Feb 2022, 17:13

I did it:

Code: Select all

#SingleInstance, Force
Gui, Show, x700 y150 h296 w300, New GUI Window
Gui, Add, Edit, x46 y50 w100 h30 vMyEdit1,

gosub, ButtonLoad
OnExit, ExitSub
Return

ButtonSave:
Gui, Submit, NoHide
IniWrite, %MyEdit1%, settings.ini, history, edit1
Return

ButtonLoad:
IniRead, MyEdit1, settings.ini, history, edit1
GuiControl, , MyEdit1, %MyEdit1%
Return

ExitSub:
gosub, ButtonSave
ExitApp
I just Implemented into my other GUI, can you help:

Code: Select all

#SingleInstance, Force
Gui, Font, s17 cOlive, Ink Free
Gui, Show, X1211 Y305 w485 h800
Gui, Add, Text,, `n`n
Gui, Add, Edit, r12 w450 vEditl -VScroll, 1) `n2) `n3) `n4) `n5) `n6) `n7) `n8) `n9) `n10) `n`n`n`n
Gui, color,,ECD4C2
Gui, color, 9CB4B3
Gui, +Alwaysontop 
OnMessage( 0x200, "WM_MOUSEMOVE" ) 
WinSet, Style, -0xC00000, A
WinSet, Region, 0-100 300-306 w610 H500 R100-100, ahk_class AutoHotkeyGUI
gosub, ButtonLoad
return

WM_MOUSEMOVE( wparam, lparam, msg, hwnd )
{
	if wparam = 1 ; LButton
		PostMessage, 0xA1, 2,,, A ; WM_NCLBUTTONDOWN
}

WheelUp::Up
WheelDown::Down

ButtonSave:
Gui, Submit, NoHide
IniWrite, %edit1%, GUI.ini, history, edit1
Return

ButtonLoad:
IniRead, edit1, GUI.ini, history, edit1
GuiControl, , MyEdit1, %MyEdit1%
Return

guiclose:
ExitApp
return
Esc::ExitApp
Does it matter if I use vMyEdit1 or vEdit1?
amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Learning IniWrite & IniRead

25 Feb 2022, 17:53

You may use any variables names as you like. MyEdit1 or Edit1 all are nice, obviously. But since you've chosen Edit1 these line becomes buggy:
Spoiler
And you've forgotten about OnExit of OnExit() to save Edit content before you close your gui. Read about it here: OnExit.
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.
LAPIII
Posts: 671
Joined: 01 Aug 2021, 06:01

Re: Learning IniWrite & IniRead

26 Feb 2022, 19:24

I just can't get this to write an ini file:

Code: Select all

#Singe downleInstance, Force
OnExit, ExitSub
Gui, Show, x825 y163 w485 h800																		; GUI
Gui, color, 9CB4B3																					; GUI background color
Gui, Font, s18 cOlive, Ink Free																		; font
Gui, +Alwaysontop 																					; always on top
OnMessage( 0x200, "WM_MOUSEMOVE" ) 																	; drag
WinSet, Style, -0xC00000, A																			; no title bar
Gui, Add, Text,, `n`n																				; blank lines
Gui, Add, Edit, r10 w400 vEditl -VScroll, 1) `n2) `n3) `n4) `n5) `n6) `n7) `n8) `n9) `n10)			; text box without scrollbar
Gui, color,,ECD4C2																					; text box background color
WinSet, Region, 0-100 300-300 w555 H675 R100-100, ahk_class AutoHotkeyGUI							; GUI shape
Gui, Add, Button, x46 y450 w100 h40 , Save
Gui, Add, Button, x166 y450 w100 h40 , Load
gosub, ButtonLoad 

WM_MOUSEMOVE( wparam, lparam, msg, hwnd )															; drag			
{
	if wparam = 1 ; LButton
		PostMessage, 0xA1, 2,,, A ; WM_NCLBUTTONDOWN
}

WheelUp::Up																							; scroll
WheelDown::Down

ButtonSave:																							; button
Gui, Submit, NoHide
IniWrite, %MyEdit1%, GUI.ini, history, edit1
Return

ButtonLoad:																							; button
IniRead, MyEdit1, GUI.ini, history, edit1
GuiControl, , MyEdit1, %MyEdit1%
Return

GuiClose:
ExitApp

ExitSub:
gosub, ButtonSave
Enter::ExitApp

The GUI will show for a split second If I use ExitApp in line 16.
User avatar
boiler
Posts: 17328
Joined: 21 Dec 2014, 02:44

Re: Learning IniWrite & IniRead

26 Feb 2022, 19:34

You are trying to reference a control named MyEdit1, but it is not named that. You left it named Editl, and that is not Edit1 with the number 1, it is the Editl with a lowercase letter L. So first get your ones and Ls straightened out. Then apparently you’re missing a key point from your last question: It doesn’t matter what you name your variable: Edit1, Editl, MyEdit1, or anything else, but you must reference what you actually named it when you defined the control using the v option.
LAPIII
Posts: 671
Joined: 01 Aug 2021, 06:01

Re: Learning IniWrite & IniRead

04 Mar 2022, 21:35

I was confused on syntax. I changed my file name to GUI.ini, section to Section, key to GUI key, and ControlID to GUIID. Therefore I have changed these lines:

Code: Select all

Gui, Add, Edit, r10 w400 vMyGUI -VScroll

Code: Select all

IniWrite, %MyGUI%, GUI.ini, Section, GUI key

Code: Select all

IniRead, MyGUI, GUI.ini, Section, GUI key 
GuiControl, , GUIID, %MyGUI%
But the scripts still won't save the ini file. Why? Here's the whole script:

Code: Select all

#SingleInstance, Force
Gui, Show, x825 y163 w485 h800												; GUI
Gui, color, 9CB4B3															; GUI background color
Gui, Font, s18 cOlive, Ink Free												; font
Gui, +Alwaysontop 															; always on top
OnMessage( 0x200, "WM_MOUSEMOVE" ) 											; drag
WinSet, Style, -0xC00000, A													; no title bar
Gui, Add, Text,, `n`n														; blank lines
Gui, Add, Edit, r10 w400 vMyGUI -VScroll									; text box without scrollbar
Gui, color,,ECD4C2															; text box background color
WinSet, Region, 0-100 300-300 w555 H675 R100-100, ahk_class AutoHotkeyGUI	; GUI shape
Gui, Add, Button, x46 y450 w100 h40 , Save
Gui, Add, Button, x166 y450 w100 h40 , Load
gosub, ButtonLoad 
OnExit, ExitSub
Return

WM_MOUSEMOVE( wparam, lparam, msg, hwnd )									; drag			
{
	if wparam = 1 ; LButton
		PostMessage, 0xA1, 2,,, A ; WM_NCLBUTTONDOWN
}

WheelUp::Up																	; scroll
WheelDown::Down

ButtonSave:																	; IniWrite
Gui, Submit, NoHide
IniWrite, %MyGUI%, GUI.ini, Section, GUI key 
Return

ButtonLoad:																	; IniRead
IniRead, MyGUI, GUI.ini, Section, GUI key 
GuiControl, , GUIID, %MyGUI%
Return

ExitSub:
gosub, ButtonSave
Enter::ExitApp
Hardcoder
Posts: 278
Joined: 19 Feb 2022, 13:13
Location: Germany

Re: Learning IniWrite & IniRead

04 Mar 2022, 21:50

It works for me though :think:

Code: Select all

[Section]
GUI key=asdfsadf
At least saving works, that is, loading does not.
Hardcoder
Posts: 278
Joined: 19 Feb 2022, 13:13
Location: Germany

Re: Learning IniWrite & IniRead

04 Mar 2022, 22:02

Your ini read should rather look like this

Code: Select all

IniRead, iniValue, GUI.ini, Section, GUI key 
GuiControl, Text, MyGUI, %iniValue%
...for that single value.

The exit behavior is weird, below gosub, ButtonSave should be an ExitApp, as that from Enter::ExitSub is only for Enter.
And .ini files are for simple values, not sure if you can actually load multiline text, for that I'd do sth like:

Code: Select all

iniValue := StrReplace(MyGUI, "`n", "``n")
<save>
and

Code: Select all

<load>
iniValue := StrReplace(iniValue, "``n", "`n")
GuiControl, Text, MyGUI, %iniValue%
LAPIII
Posts: 671
Joined: 01 Aug 2021, 06:01

Re: Learning IniWrite & IniRead

07 Mar 2022, 13:41

Hardcoder wrote:
04 Mar 2022, 21:50

Code: Select all

[Section]
GUI key=asdfsadf

When I tried to put this into my script, it said that this isn't a recognized action.

Hardcoder wrote:
04 Mar 2022, 22:02
Your ini read should rather look like this

Code: Select all

IniRead, iniValue, GUI.ini, Section, GUI key 
GuiControl, Text, MyGUI, %iniValue%
...for that single value.

The exit behavior is weird, below gosub, ButtonSave should be an ExitApp, as that from Enter::ExitSub is only for Enter.

Enter::ExitSub didn't work so I just used ExitSub.


Here's my script with your other suggestions (the script still doesn't save or load):

Code: Select all

#SingleInstance, Force
Gui, Show, x825 y163 w485 h800												; GUI
Gui, color, 9CB4B3															; GUI background color
Gui, Font, s18 cOlive, Ink Free												; font
Gui, +Alwaysontop 															; always on top
OnMessage( 0x200, "WM_MOUSEMOVE" ) 											; drag
WinSet, Style, -0xC00000, A													; no title bar
Gui, Add, Text,, `n`n														; blank lines
Gui, Add, Edit, r10 w400 vMyGUI -VScroll									; text box without scrollbar
Gui, color,,ECD4C2															; text box background color
WinSet, Region, 0-100 300-300 w555 H675 R100-100, ahk_class AutoHotkeyGUI	; GUI shape
Gui, Add, Button, x46 y450 w100 h40 , Save
Gui, Add, Button, x166 y450 w100 h40 , Load
gosub, ButtonLoad 
OnExit, ExitSub
Return

WM_MOUSEMOVE( wparam, lparam, msg, hwnd )									; drag			
{
	if wparam = 1 ; LButton
		PostMessage, 0xA1, 2,,, A ; WM_NCLBUTTONDOWN
}

WheelUp::Up																	; scroll
WheelDown::Down

ButtonSave:																	; IniWrite
Gui, Submit, NoHide
IniWrite, %MyGUI%, GUI.ini, Section, GUI key 
Return

ButtonLoad:																	; IniRead
IniRead, iniValue, GUI.ini, Section, GUI key 
GuiControl, , GUIID, %MyGUI%
Return

iniValue := StrReplace(MyGUI, "`n", "``n")
<save>

<load>
iniValue := StrReplace(iniValue, "``n", "`n")
GuiControl, Text, MyGUI, %iniValue%

ExitSub:
gosub, ButtonSave
ExitApp
Hardcoder
Posts: 278
Joined: 19 Feb 2022, 13:13
Location: Germany

Re: Learning IniWrite & IniRead

07 Mar 2022, 14:17

You left my placeholders <save> and <load> in the script, they were meant to be replaced by your code to save/load.
When I place the code above/below them at the respective positions, it works. :thumbup:
I don't post the corrections, you need to understand why and where, it's not much to do, and these placeholders do tell you.
LAPIII
Posts: 671
Joined: 01 Aug 2021, 06:01

Re: Learning IniWrite & IniRead

07 Mar 2022, 15:34

Thank you very much. Two questions though:
  1. Where is the ini file saved?
  2. If I were to add a hotkey, how would you close the GUI to keep this script running?
RussF
Posts: 1303
Joined: 05 Aug 2021, 06:36

Re: Learning IniWrite & IniRead

07 Mar 2022, 15:39

From IniWrite:
Filename
The name of the .ini file, which is assumed to be in %A_WorkingDir% if an absolute path isn't specified.
Russ
Hardcoder
Posts: 278
Joined: 19 Feb 2022, 13:13
Location: Germany

Re: Learning IniWrite & IniRead

07 Mar 2022, 16:02

LAPIII wrote:
07 Mar 2022, 15:34
Thank you very much. Two questions though:
  1. Where is the ini file saved?
  2. If I were to add a hotkey, how would you close the GUI to keep this script running?
Russ anwered 1.
2. You issue a Gui, Hide, or a Gui, Submit with no explicit NoHide.
RussF
Posts: 1303
Joined: 05 Aug 2021, 06:36

Re: Learning IniWrite & IniRead

07 Mar 2022, 16:17

Thanks @Hardcoder, I hit submit and forgot all about the second question. :oops:

Russ
LAPIII
Posts: 671
Joined: 01 Aug 2021, 06:01

Re: Learning IniWrite & IniRead

07 Mar 2022, 19:18

How would I assign a file path to save the ini file?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 74 guests