Jump to content

Saving Check Box state to config.ini file ( Need Help )


thawee
 Share

Recommended Posts

Hi, All

I have been working on saving check box states to a configuration file.

I have noticed that what is save is neither 0 or a 1.

The problem I am having is I want to read the data in from the config file, and set the check box state back to what it was after the check boxes were checked or not checked.

For example

IniWrite("C:\Config.txt","Section1","sec1",$Checkbox1)

$Checkbox1 = IniRead("C:\Config.txt","Section1","sec1",$Checkbox1)

Is there anything more i need to do to set the state of the check box to checked.

Please advise,

Thanks

Thawee

Edited by thawee
Link to comment
Share on other sites

  • Moderators

thawee,

You are saving the ControlID of the checkbox, not its state. :)

When you create a control, AutoIt returns its ControlID, an integer value you then use to identify the control. If you want to know the state of a control, you need to use GUIGetState.

Here is a short example to help you see what I mean:

#include <GUIConstantsEx.au3>

$hGUI = GUICreate("Test", 500, 500)

$hButton_1 = GUICtrlCreateButton("Test", 10, 10, 80, 30)
; $hButton is the ControlId

$hCheckBox = GUICtrlCreateCheckBox(" Check me!", 10, 80, 100, 20)

$hButton_2 = GUICtrlCreateButton("Read", 10, 110, 80, 30)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hButton_1 ; The ControlID is used here
            MsgBox(0, "ControlID", "The ControlID of this button is " & $hButton_1)
        Case $hButton_2 ; The ControlID is used here
            $sMsg = "The ControlID of this button is " & $hButton_2 & @CRLF & @CRLF & _
                    "The state of the checkbox is " & GUICtrlRead($hCheckBox) ; Read the STATE of the checkbox
            MsgBox(0, "ControlID", $sMsg)

    EndSwitch

WEnd

So you need to save the return value from GUICtrlRead and then use that value with GUICtrlSetState to get it as it was.

Ask if anything is unclear. :idea:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thanks for the reply!

So, I would assume that to write the state of the Check box to a ini file I would need to do something similar to this.

$CBox-State = GUICtrlRead($hCheckBox)

; Writing
IniWrite("C:\Config.txt","Section1","sec1",$CBox-State)

; Reading and setting state
$CBox-State = IniRead("C:\Config.txt","Section1","sec1","Not Found")
GuictrlSetState($hCheckBox,$CBox-State)

Does that seem like what would need to happen to successfully preserve the state of a check box in a ini file?

Edited by thawee
Link to comment
Share on other sites

  • Moderators

thawee,

Have you tried? :idea:

M23

Fix the syntax errors and it should work!

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Here's another little trick for you

$CBox_State = BitAND(GUICtrlGetState($hCheckBox), $GUI_Checked) = $GUI_Checked
IniWrite("C:\Config.txt","Section1","sec1",$CBox_State)

That will write it as 1 or 0

Then you can just use

$CBox_State = IniRead("C:\Config.txt","Section1","sec1","")
If $CBox_State Then GuictrlSetState($hCheckBox,$CBox_State)

Note: You can't use a hyphen in a variable name, stick with letters, digits or the underscore character for best success.

EDIT: Fix the spellink before M23 has something to pick on me about.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • 1 month later...

Thawee, I'm having the same questions... I think this part (cleaned up of course) would be more elegant than doing another conversion... how did this turn out?

I'm leaning toward doing it into the registry, but otherwise I'd like to do the same thing.

Thanks for the reply!

So, I would assume that to write the state of the Check box to a ini file I would need to do something similar to this.

$CBox-State = GUICtrlRead($hCheckBox)

; Writing
IniWrite("C:\Config.txt","Section1","sec1",$CBox-State)

; Reading and setting state
$CBox-State = IniRead("C:\Config.txt","Section1","sec1","Not Found")
GuictrlSetState($hCheckBox,$CBox-State)

Does that seem like what would need to happen to successfully preserve the state of a check box in a ini file?

Link to comment
Share on other sites

It should be working but I just noticed a mistake in the code he posted back

$CBox-State = IniRead("C:\Config.txt","Section1","sec1","Not Found")

GuictrlSetState($hCheckBox,$CBox-State)

Would be better as

$CBox-State = IniRead("C:\Config.txt","Section1","sec1", 4);; Set a default value here
GuictrlSetState($hCheckBox,$CBox-State)

OR

$CBox-State = IniRead("C:\Config.txt","Section1","sec1", 0);; Set a default value of 0 here
If $CBox-State Then GuictrlSetState($hCheckBox,$CBox-State)

Also If I were you, I would try to avoid the registry. It already becomes bloated enough as it is so it's prone to being a bit slower and remember most of that registry will also be read at system startup whether or not it's needed.

I would stay with the ini and in order to avoid write permission issues store the ini in @AppDataDir or @AppDataCommonDir

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...