Skeletor

Problems with GuiCtrlSetState

8 posts in this topic

#1 ·  Posted

Hi Guys,

I'm having abit of a problem trying to read the information back to my GUI program. 
I'm not sure what I'm doing wrong here. Please help. 
 

So, I have this code below. I click on the checked boxes and save the file. That's fine, until I try open the file to read the information back I end up with everything been read incorrect. Test the code and see for yourself. What is it that I'm doing wrong?

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1_1 = GUICreate("Form1", 211, 152, 192, 104)
$MenuItem1 = GUICtrlCreateMenu("&File")
$MenuItem2 = GUICtrlCreateMenuItem("New"&@TAB&"F9", $MenuItem1)
$MenuItem3 = GUICtrlCreateMenuItem("Open"&@TAB&"", $MenuItem1)
$MenuItem4 = GUICtrlCreateMenuItem("Save", $MenuItem1)
$Checkbox1 = GUICtrlCreateCheckbox("Yes", 107, 16, 50, 17)
$Checkbox2 = GUICtrlCreateCheckbox("No", 159, 16, 50, 17)
$Input1 = GUICtrlCreateInput("", 11, 16, 89, 21)
$Input2 = GUICtrlCreateInput("", 11, 39, 89, 21)
$Checkbox3 = GUICtrlCreateCheckbox("Yes", 107, 39, 50, 17)
$Checkbox4 = GUICtrlCreateCheckbox("No", 159, 39, 50, 17)
$Input3 = GUICtrlCreateInput("", 11, 63, 89, 21)
$Checkbox5 = GUICtrlCreateCheckbox("Yes", 107, 63, 50, 17)
$Checkbox6 = GUICtrlCreateCheckbox("No", 159, 63, 50, 17)
Dim $Form1_1_AccelTable[1][2] = [["{F9}", $MenuItem2]]
GUISetAccelerators($Form1_1_AccelTable)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###





While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $MenuItem2
            _newFile()
        Case $MenuItem3
            _OpenFile()
        Case $MenuItem4
            _SaveFile()
    EndSwitch
WEnd

Func _newFile()


EndFunc


Func _OpenFile()
$FileOpen = FileOpenDialog("Open File",@ScriptDir,"Custom File (*.cf)")
$FileOpen2 = FileOpen($FileOpen)
$FileRead = FileReadLine($FileOpen2)
If $FileRead = "Checked" Then
GUICtrlSetState($Checkbox1,1)
Else
    GUICtrlSetState($Checkbox2,1)
EndIf
If $FileRead = "Checked" Then
GUICtrlSetState($Checkbox3,1)
Else
    GUICtrlSetState($Checkbox4,1)
EndIf
If $FileRead = "Checked" Then
GUICtrlSetState($Checkbox5,1)
Else
    GUICtrlSetState($Checkbox6,1)
EndIf





EndFunc



Func _SaveFile()
    If GUICtrlRead($Checkbox1) = 1 Then
    $State1 = "Checked"
Else
    $State1 = ""
EndIf

If GUICtrlRead($Checkbox3) = 1 Then
    $State3 = "Checked"
Else
    $State3 = ""
EndIf

If GUICtrlRead($Checkbox5) = 1 Then
    $State5 = "Checked"
Else
    $State5 = ""
EndIf

$FileSave = FileSaveDialog("Save File",@ScriptDir,"Custom File (*.cf)",16)
FileWrite($FileSave,$State1 & @CRLF)
FileWrite($FileSave,$State3 & @CRLF)
FileWrite($FileSave,$State5 & @CRLF)

EndFunc

 


Kind Regards
Skeletor

"I need coffee to turn me back to a normal human"

Share this post


Link to post
Share on other sites



#2 ·  Posted

@Skeletor

Good evening :)

First, I suggest you a bit of identation, in order to have a better reading of your code :)

Then, let's talk about what is wrong in your code:

1) You should use a state for Not Checked checkboxes too, not just a blank string;

2) With this line of code

1 hour ago, Skeletor said:

$FileRead = FileReadLine($FileOpen2)

You just read the first line of your file, and so ( as I tested, putting the first checkbox on the state "Checked" ), you have all the checkboxes checked or unchecked, just because you don't read all the lines of your file, but just the first one. So, I suggest you to read the file in a loop or using some function like FileRead() or _FileReadToArray(), in order to have all the lines of your file, and compare them in a loop, with If...Else...EndIf condition :)

Something like this ( replacing ConsoleWrite() with the set of the state of your checkboxes ):

; $strFile and $arrReturn declared before!
_FileReadToArray($strFile, $arrReturn)
For $i = 1 To UBound($arrReturn) - 1
    If $arrReturn[$i] = "Checked" Then
        ConsoleWrite("Is Checked!" & @CRLF)
    Else
        ConsoleWrite("Is Not Checked!" & @CRLF)
    EndIf
Next

Hope I've been clearer enough :)

Tell us if you managed to do what you're trying to do :) 


Click here to see my signature:

Spoiler

I will always thank you for the time you spent for me.
I'm here to ask, and from your response, I'd like to learn.
By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

Share this post


Link to post
Share on other sites

#3 ·  Posted

I've used the FileReadToArray(FileHandle) function... The consolewrite works, but I just cant set the Checkbox correctly when using GuiCtrlSetState.
What could be the problem?

$FileOpen = FileOpenDialog("Open File",@ScriptDir,"Custom File (*.cf)")

$aArray = FileReadToArray($FileOpen)
For $i = 0 To UBound($aArray) - 1
    If $aArray[$i] = "Checked" Then
        GUICtrlSetState($Checkbox1,1)
    Else
        GUICtrlSetState($Checkbox1,0)
    EndIf
Next

 


Kind Regards
Skeletor

"I need coffee to turn me back to a normal human"

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I see the issue. When the array sifts through the list, it does so quickly which means only the ConsoleWrite and MsgBox which I tested so far actually reads out each line. Basically the msgbox will display the line1, user presses ok, then array moves to the next line, etc...

So now I need to find a way to have each line read, set the checkbox then move to the next line...hmmm, interesting to investigate.

Edited by Skeletor

Kind Regards
Skeletor

"I need coffee to turn me back to a normal human"

Share this post


Link to post
Share on other sites

#5 ·  Posted

OK I tested out my script and came out with this:

$FileOpen = FileOpenDialog("Open File",@ScriptDir,"Custom File (*.cf)")
While 1
$FileRead1 = FileReadLine($FileOpen)
    If $FileRead1 = "Checked" Then
        GUICtrlSetState($Checkbox1,1)
    Else
        GUICtrlSetState($Checkbox2,1)
    EndIf

ExitLoop
WEnd

The problem I have with arrays is it reads it to quickly therefore only ConsoleWrite can diplay the list correctly and MsgBox can read each line to each dialogue box.

However, in cases like GuiCtrlSetState, then I'm seeing the FileReadLine actually pull through the correct state from my generated file I have called Custom File.
My next task is to have the entire list read, line by line and set each CheckBox correctly.


Kind Regards
Skeletor

"I need coffee to turn me back to a normal human"

Share this post


Link to post
Share on other sites

#6 ·  Posted

I got this working but I know there is a better way to it. However, I've noticed in coding there is either a quicker way or longer way... either way you still get the same results:

$FileOpen = FileOpenDialog("Open File",@ScriptDir,"Custom File (*.cf)")
While 1
$FileRead1 = FileReadLine($FileOpen,1)
    If $FileRead1 = "Checked" Then
        GUICtrlSetState($Checkbox1,1)
    Else
        GUICtrlSetState($Checkbox2,1)
    EndIf
$FileRead1 = FileReadLine($FileOpen,2)
        If $FileRead1 = "Checked" Then
        GUICtrlSetState($Checkbox3,1)
    Else
        GUICtrlSetState($Checkbox4,1)
    EndIf
$FileRead1 = FileReadLine($FileOpen,3)
        If $FileRead1 = "Checked" Then
        GUICtrlSetState($Checkbox5,1)
    Else
        GUICtrlSetState($Checkbox6,1)
    EndIf

ExitLoop
WEnd

 


Kind Regards
Skeletor

"I need coffee to turn me back to a normal human"

Share this post


Link to post
Share on other sites

#7 ·  Posted

Hello. Consider to use ini files functions. 

 

Saludos

Share this post


Link to post
Share on other sites

#8 ·  Posted

@Danyfirex, Thank you for the suggestion. I have thought about this however, the only draw back was a custom file was needed.  In the final build of this program, the Custom File (*.cf) would be replaced with a different file system which would be associated to the program.

Just like if you had to save a word document, you can open the .docx file in the MS Word executable.


Kind Regards
Skeletor

"I need coffee to turn me back to a normal human"

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