Sign in to follow this  
Followers 0
hendrikhe

How to save and load Values?

7 posts in this topic

I want to give my programm a save and load option (for a checkbox and combo), so I dont need to configure the options all the time. But I dont know the functions to realize it. Can someone explain it to me with this simple script:

CODE
#include <ButtonConstants.au3>

#include <ComboConstants.au3>

#include <GUIConstantsEx.au3>

#include <WindowsConstants.au3>

#Region ### START Koda GUI section ### Form=

$Form1 = GUICreate("Form1", 633, 447, 193, 125)

$Combo1 = GUICtrlCreateCombo("Combo1", 16, 80, 145, 25)

GUICtrlSetData(-1,"800x600|1024x768|1152x864|1280x768|1280x960|1280x1024","")

$Load = GUICtrlCreateButton("Load", 416, 48, 75, 25, 0)

$Save = GUICtrlCreateButton("Save", 416, 104, 75, 25, 0)

$Enable = GUICtrlCreateCheckbox("Enable options", 16, 48, 97, 17)

GUISetState(@SW_SHOW)

#EndRegion ### END Koda GUI section ###

While 1

$nMsg = GUIGetMsg()

Switch $nMsg

Case $GUI_EVENT_CLOSE

Exit

EndSwitch

WEnd

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Do you mean something like this:

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 633, 447, 193, 125)
$Combo1 = GUICtrlCreateCombo("Combo1", 16, 80, 145, 25)
GUICtrlSetData(-1, "800x600|1024x768|1152x864|1280x768|1280x960|1280x1024", "")
$Load = GUICtrlCreateButton("Load", 416, 48, 75, 25, 0)
$Save = GUICtrlCreateButton("Save", 416, 104, 75, 25, 0)
$Enable = GUICtrlCreateCheckbox("Enable options", 16, 48, 97, 17)
GUISetState(@SW_SHOW)
#EndRegion ### START Koda GUI section ### Form=

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
            
        Case $Load
            Load_It()

        Case $Save
            Save_It()

    EndSwitch
WEnd

Func Load_It()
    If BitAND(GUICtrlRead($Enable), $GUI_CHECKED) = $GUI_CHECKED Then
        MsgBox(0, "", "Options are enabled.")
    EndIf
EndFunc

Func Save_It()
    If BitAND(GUICtrlRead($Enable), $GUI_CHECKED) = $GUI_CHECKED Then
        MsgBox(0, "", "Options are enabled.")
    EndIf
EndFunc

Probably you mean this:

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 633, 447, 193, 125)
$Combo1 = GUICtrlCreateCombo("Combo1", 16, 80, 145, 25)
GUICtrlSetData(-1, "800x600|1024x768|1152x864|1280x768|1280x960|1280x1024", "")
$Load = GUICtrlCreateButton("Load", 416, 48, 75, 25, 0)
$Save = GUICtrlCreateButton("Save", 416, 104, 75, 25, 0)
$Enable = GUICtrlCreateCheckbox("Enable options", 16, 48, 97, 17)
GUISetState(@SW_SHOW)
#EndRegion ### START Koda GUI section ### Form=

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
            
        Case $Load
            Load_It()

        Case $Save
            Save_It()

    EndSwitch
WEnd

Func Load_It()
    If GUICtrlRead($Combo1) <> "Combo1" Then
        MsgBox(0, "", "Options are enabled.")
    Else
        MsgBox(0, "", "Options are NOT enabled.")
    EndIf
EndFunc

Func Save_It()
    If GUICtrlRead($Combo1) <> "Combo1" Then
        MsgBox(0, "", "Options are enabled.")
    Else
        MsgBox(0, "", "Options are NOT enabled.")
    EndIf
EndFunc
Edited by Squirrely1

Das Häschen benutzt Radar

Share this post


Link to post
Share on other sites

No I dont mean this. If I open the programm and choose on the combo box for example 1024x768 (or I activate the checkbox), after that I press "Save", so my settings are saved somewhere. I close the programm, reopen it press "Load" and my settings from the last session which are saved get loaded, so I dont need to choose 1024x768 again. Thats what I need

Share this post


Link to post
Share on other sites

with iniwrite and iniread you can store your information in another file and have it load the information on startup.

i actually used this on a recent project i made. it was an Alarm Clock, and i use iniwrite and iniread to save and load the information the person inputted in the input boxes. :)

Alarm Clock

Share this post


Link to post
Share on other sites

No I dont mean this. If I open the programm and choose on the combo box for example 1024x768 (or I activate the checkbox), after that I press "Save", so my settings are saved somewhere. I close the programm, reopen it press "Load" and my settings from the last session which are saved get loaded, so I dont need to choose 1024x768 again. Thats what I need

look at IniRead(), IniWrite() and other Ini file functions

create your ini file in the folder with the script

also look at Reg functions

Opt('MustDeclareVars', 1)
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

#Region ### START Koda GUI section ### Form=
Local $IniFile = @ScriptDir & "\" & StringTrimRight(@ScriptName, 3) & "ini"
Local $IniRead, $nMsg
Local $Form1 = GUICreate("Form1", 633, 447, 193, 125)
Local $Combo1 = GUICtrlCreateCombo("Combo1", 16, 80, 145, 25)
GUICtrlSetData(-1, "800x600|1024x768|1152x864|1280x768|1280x960|1280x1024", "")
Local $Load = GUICtrlCreateButton("Load", 416, 48, 75, 25, 0)
Local $Save = GUICtrlCreateButton("Save", 416, 104, 75, 25, 0)
Local $Enable = GUICtrlCreateCheckbox("Enable options", 16, 48, 97, 17)
GUISetState(@SW_SHOW)
#EndRegion ### START Koda GUI section ### Form=

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $Save
            _IniOptions($Save)
        Case $Load
            _IniOptions($Load)
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

; as long as variables used in this function are declared local or global to entire script
; then no need to pass extra parameters to the function, i.e. $Enable, $Combo1, etc.
Func _IniOptions($iMsg)
    Local $res
    If BitAND(GUICtrlRead($Enable), $GUI_CHECKED) = $GUI_CHECKED Then
        Switch $iMsg
            Case $Save
                $res = GUICtrlRead($Combo1)
                IniWrite($IniFile, "Display Properties", "Resolution", $res)
            Case $Load
                ; last parameter '1024x768' is default value if nothing found in ini file
                $IniRead = IniRead($IniFile, "Display Properties", "Resolution", "1024x768")
                GUICtrlSetData($Combo1,$IniRead)
        EndSwitch
    EndIf
EndFunc

I see fascists...

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

this will optionally load your saved settings on start

without need to load

Edit: some additional suggestions to not use combo title, use default values, etc.

Opt('MustDeclareVars', 1)
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

#Region ### START Koda GUI section ### Form=
Local $IniFile = @ScriptDir & "\" & StringTrimRight(@ScriptName, 3) & "ini"
Local $IniRead, $nMsg
Local $Form1 = GUICreate("Form1", 633, 447, 193, 125)
; don't use title for combo if not a useable item
Local $Combo1 = GUICtrlCreateCombo("", 16, 80, 145, 25)
; set default combo value as last parameter
GUICtrlSetData(-1, "800x600|1024x768|1152x864|1280x768|1280x960|1280x1024", "1024x768")
Local $Load = GUICtrlCreateButton("Load", 416, 48, 75, 25, 0)
Local $Save = GUICtrlCreateButton("Save", 416, 104, 75, 25, 0)
Local $Enable = GUICtrlCreateCheckbox("Enable options", 16, 48, 97, 17)
GUISetState(@SW_SHOW)
#EndRegion ### START Koda GUI section ### Form=

; load saved settings from ini
If FileExists($IniFile) Then
    If IniRead($IniFile, "Options", "Enable", 0) = $GUI_CHECKED Then
        GUICtrlSetState ($Enable, $GUI_CHECKED)
        _IniOptions($Load)
    EndIf
EndIf

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $Save
            _IniOptions($Save)
        Case $Load
            _IniOptions($Load)
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

; as long as variables used in this function are declared local or global to entire script
; then no need to pass extra parameters to the function, i.e. $Enable, $Combo1, etc.
Func _IniOptions($iMsg)
    Local $res
    Switch $iMsg
        Case $Save
            $res = GUICtrlRead($Combo1)
            IniWrite($IniFile, "Display Properties", "Resolution", $res)
            If BitAND(GUICtrlRead($Enable), $GUI_CHECKED) = $GUI_CHECKED Then
                IniWrite($IniFile, "Options", "Enable", $GUI_CHECKED)
            Else
                IniWrite($IniFile, "Options", "Enable", $GUI_UNCHECKED)
            EndIf
        Case $Load
            If BitAND(GUICtrlRead($Enable), $GUI_CHECKED) = $GUI_CHECKED Then
                ; last parameter '1024x768' is default value if nothing found in ini file
                $IniRead = IniRead($IniFile, "Display Properties", "Resolution", "1024x768")
                GUICtrlSetData($Combo1,$IniRead)
            EndIf
    EndSwitch
EndFunc
Edited by rover

I see fascists...

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Thnx, i will try it :)

--Edit--

Thnx you helped me alot!

Edited by hendrikhe

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0