Jump to content

Problem with GUICtrlRead in second GUI


JB72
 Share

Go to solution Solved by abberration,

Recommended Posts

Hi,

i wrote this little script to easily select VBS scripts via radio button to test them. The scripts are in a directory, which is read out and you can then use a radio button to select which one you want to edit. That also works so far.

;chooseTestScript.au3
#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $aArray = _FileListToArrayRec("C:\scripts", "Setup.vbs||*_bak*", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_SORT)
            
$ScriptChoose = GUICreate("Choose :", 500, 500)
$top = 10
 
   For $ar = 1 To $aArray[0]
       GUICtrlCreateRadio($aArray[$ar], 10, $top, 350,20)
        $top += 30
    Next
    
$ScriptChooseButton = GUICtrlCreateButton("OK", 325, 450, 115, 35, 0)

GUISetState(@SW_SHOW, $ScriptChoose)
While 1
    $nMsg2 = GUIGetMsg($ScriptChoose)
    Switch $nMsg2
        Case $GUI_EVENT_CLOSE
            Exit
        Case $ScriptChooseButton
                For $ar = 1 To $aArray[0]
                    If GUICtrlRead($ar + 2) = $GUI_CHECKED Then
                    MsgBox(0, "", "Your choise : " & $aArray[$ar])
                    $verzeichnis = "scripts" & $aArray[$ar]
                    Iniwrite(@scriptdir&"\verzeichnis.ini", "Verzeichnis", "VBS", $verzeichnis)
                    $verzeichnis =   "C:\scripts\" & StringRegExpReplace($aArray[$ar], "\\Setup.vbs", "")
                    Iniwrite(@scriptdir&"\verzeichnis.ini", "Verzeichnis", "Pfad", $verzeichnis)
                    GUIDelete ($ScriptChoose)
                    Exit
;~                     ExitLoop
                EndIf
            Next
    EndSwitch
WEnd


Now I thought that works well so far, then I'll build it into my main gui. And that's where I'm failing. I'll post a part of my main script here.

If I press the button so that the selection window (Case $RadioChoose) with the radio buttons is displayed, everything works so far. I make my selection and then press the button for the selected script ($ScriptChooseButton), nothing happens. The message box doesn't come either. I assume this is due to the two GUIs and that I have problems with "If GUICtrlRead" because of the two GUIs.
 

Case $RadioChoose   
            GUISetState(@SW_SHOW,$ChooseScript)
            GUICtrlSetState($ChooseScript,$GUI_FOCUS)
            Global $aArray = _FileListToArrayRec($verzeichnisRadio & "\", "Setup.vbs||*_bak*", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_SORT)
            $top = 10
 
            For $ar = 1 To $aArray[0]
            GUICtrlCreateRadio($aArray[$ar], 10, $top, 350,20)
            ;MsgBox(0,"",$aArray[$ar])
            $top += 30
            Next
            
                
Case $ScriptChooseButton
                 
                
                For $ar = 1 To $aArray[0]
                    
                
                    If GUICtrlRead($ar + 2) = $GUI_CHECKED Then
                    
                    MsgBox(0, "", "Auswahl : " & $aArray[$ar])
                    $verzeichnis = "C:\scripts\" & $aArray[$ar]
                    Iniwrite(@scriptdir&"\verzeichnis.ini", "Verzeichnis", "VBS", $verzeichnis)
                    $verzeichnis =   "C:\scripts\" & StringRegExpReplace($aArray[$ar], "\\Setup.vbs", "")
                    Iniwrite(@scriptdir&"\verzeichnis.ini", "Verzeichnis", "Pfad", $verzeichnis)
                    GUIDelete ($ChooseScript)
                    
                    EndIf
                Next

If I output something in the $ScriptChooseButton in the FOR loop, it works. But from the "IF" nothing happens.

A directory with empty VBS files is included in the attached zip as an example.


Greetings JB


 

 

scripts.zip

Link to comment
Share on other sites

The problem you are encountering is that you are not assigning the radio buttons to a variable. The easiest way to do this is with Assign and Eval (listed in the help file). Another way is to add a column to your $aArray and assign the radio button to the 2nd column of the array when the radio is created. This method is just as good, but will make you re-work all your $aArray reads.

Anyways, I guess you will choose Assign/Eval, so this will be a good learning experience for you. Assign/Eval are great tools when you are automatically generating lots of buttons, radios, combo boxes, etc. 

Basically, when you are creating the radios, it will assign them $Radio1, $Radio2, $Radio3 and you can read them exactly with those variables or you can read them in loops with Eval("Radio" & $ar). 

Here's an example where two guis can read the same radio boxes:

#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $aArray = _FileListToArrayRec(@ScriptDir, "Setup.vbs||*_bak*", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_SORT)
$top = 10

$Form1 = GUICreate("Form1", 400, 278, 192, 124)
For $ar = 1 To $aArray[0]
    Assign("Radio" & $ar, GUICtrlCreateRadio($aArray[$ar], 10, $top, 350, 20))
    $top += 30
Next
GUICtrlSetState($Radio2, $GUI_CHECKED) ; Added this for easier testing
$Button1 = GUICtrlCreateButton("Button1", 264, 208, 75, 25, $WS_GROUP)
GUISetState(@SW_SHOW)


$Form2 = GUICreate("Form2", 300, 293, 700, 218)
$Button2 = GUICtrlCreateButton("read gui #1", 136, 136, 75, 25, $WS_GROUP)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            For $ar = 1 To $aArray[0]
                If GUICtrlRead(Eval("Radio" & $ar)) = $GUI_CHECKED Then ; Use Eval to read the radio
                    MsgBox(0, "", "Auswahl : " & $aArray[$ar])
                    $verzeichnis = "C:\scripts\" & $aArray[$ar]
                    IniWrite(@ScriptDir & "\verzeichnis.ini", "Verzeichnis", "VBS", $verzeichnis)
                    $verzeichnis = "C:\scripts\" & StringRegExpReplace($aArray[$ar], "\\Setup.vbs", "")
                    IniWrite(@ScriptDir & "\verzeichnis.ini", "Verzeichnis", "Pfad", $verzeichnis)
                EndIf
            Next
        Case $Button2
            For $ar = 1 To $aArray[0]
                If GUICtrlRead(Eval("Radio" & $ar)) = $GUI_CHECKED Then ; Same as $Button1
                    MsgBox(0, "", "Auswahl : " & $aArray[$ar])
                    $verzeichnis = "C:\scripts\" & $aArray[$ar]
                    IniWrite(@ScriptDir & "\verzeichnis.ini", "Verzeichnis", "VBS", $verzeichnis)
                    $verzeichnis = "C:\scripts\" & StringRegExpReplace($aArray[$ar], "\\Setup.vbs", "")
                    IniWrite(@ScriptDir & "\verzeichnis.ini", "Verzeichnis", "Pfad", $verzeichnis)
                EndIf
            Next
    EndSwitch
WEnd

 

Edited by abberration
Link to comment
Share on other sites

Thank you abberration !

That was a great explanation.

After I played with message boxes and saw that in script 1 correct values are displayed for a selected radio button and in my main gui wrong values (68 or 0) I had the suspicion that something was wrong with the buttons.

Two small changes bring now almost the success.  Thank you again.

When I have made a selection, I close the second GUI (back to the main window) and when I call it up again, the selection no longer works. Which is certainly due to the fact that the radio buttons have already been set up and by displaying them again, the array is filled with values again and then it doesn't match.

Case $RadioChoose
            $verzeichnisRadio = Iniread(@scriptdir&"\verzeichnis.ini", "Verzeichnis", "PfadRadio", "")
            
            GUISetState(@SW_SHOW,$ChooseScript)
            
            Global $aArray = _FileListToArrayRec($verzeichnisRadio & "\", "Setup.vbs||*_bak*", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_SORT)
            $top = 10
        
            For $ar = 1 To $aArray[0]
            Assign("Radio" & $ar, GUICtrlCreateRadio($aArray[$ar], 10, $top, 350, 20));Change 1
            $top += 30
            Next
            
            
                        
Case $ScriptChooseButton
                
                
                For $ar = 1 To $aArray[0]
                        
                    If GUICtrlRead(Eval("Radio" & $ar)) = $GUI_CHECKED Then ; Change 2
                    MsgBox(0, "", "Auswahl : " & $aArray[$ar])
                    $verzeichnis = "C:\packaging\" & $aArray[$ar]
                    Iniwrite(@scriptdir&"\verzeichnis.ini", "Verzeichnis", "VBS", $verzeichnis)
                    $verzeichnis =   "C:\packaging\" & StringRegExpReplace($aArray[$ar], "\\Setup.vbs", "")
                    Iniwrite(@scriptdir&"\verzeichnis.ini", "Verzeichnis", "Pfad", $verzeichnis)
                    
                    GUISetState(@SW_HIDE, $ChooseScript); After choosing a script,hide the second GUI
                    EndIf               
                            
                Next

Greetings JB
 

Link to comment
Share on other sites

  • Solution

OK, I think I understand the situation a little better. GUI1 should be your main GUI and GUI2 should be the radio selection (I think). Therefore you might want to structure your GUI2 to do all the reading file locations, making the radios and writing to the INI. Just for an example, I had the following code to write your selection in an INI in the script directory (and a little sample code (_WriteToINI) if you want to put INI writing off to another function). I might be wrong about all this, but here's an idea of how this might work...

#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$Form1 = GUICreate("Form1", 400, 278, 192, 124)
$Button1 = GUICtrlCreateButton("Choose", 264, 208, 75, 25, $WS_GROUP)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _CreateRadios()
    EndSwitch
WEnd

Func _CreateRadios()
    $Form2 = GUICreate("Form2", 300, 293, 700, 218)
    $top = 10
    $aArray = _FileListToArrayRec(@ScriptDir, "Setup.vbs||*_bak*", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_SORT)
    For $ar = 1 To $aArray[0]
        Assign("Radio" & $ar, GUICtrlCreateRadio($aArray[$ar], 10, $top, 350, 20))
        $top += 30
    Next
    $Button2 = GUICtrlCreateButton("Select", 136, 136, 75, 25, $WS_GROUP)
    GUISetState(@SW_SHOW)

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                GUIDelete($Form2)
                ExitLoop
            Case $Button2
                For $ar = 1 To $aArray[0]
                    If GUICtrlRead(Eval("Radio" & $ar)) = $GUI_CHECKED Then
                        IniWrite(@ScriptDir & "\verzeichnis.ini", "Verzeichnis", "VBS", GUICtrlRead(Eval("Radio" & $ar), 1))
                        _WriteToINI($aArray, $ar)
                        MsgBox(0, "", "You chose: " & GUICtrlRead(Eval("Radio" & $ar), 1) & @CRLF & "It has been written to the INI @ script folder")
                    EndIf
                Next
                GUIDelete($Form2)
                ExitLoop
        EndSwitch
    WEnd
EndFunc   ;==>_CreateRadios

Func _WriteToINI($aArray, $ar) ; Other stuff you may want to write to an INI
    $verzeichnis = "scripts" & $aArray[$ar]
    IniWrite(@ScriptDir & "\verzeichnis.ini", "Verzeichnis", "VBS", $verzeichnis)
    $verzeichnis = @ScriptDir & "\scripts\" & StringRegExpReplace($aArray[$ar], "\\Setup.vbs", "")
    IniWrite(@ScriptDir & "\verzeichnis.ini", "Verzeichnis", "Pfad", $verzeichnis)
EndFunc   ;==>_WriteToINI

 

Link to comment
Share on other sites

Yes this works fine 🙂

I work with the ISN AutoIT Studio and i use the Forms-Studio 2 Editor.

So in the "main".au3 ist the form inclueded, at the top of the script, but this does not work in this case.

The include of the created gui must be in the function now.
 

Func _CreateRadios()
    #include "Forms\ChooseScript.isf"

            $verzeichnisRadio = Iniread(@scriptdir&"\verzeichnis.ini", "Verzeichnis", "PfadRadio", "");
            
            GUISetState(@SW_SHOW,$ChooseScript)
            $aArray = _FileListToArrayRec($verzeichnisRadio & "\", "Setup.vbs||*_bak*", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_SORT)
            $top = 10
        
            For $ar = 1 To $aArray[0]
            Assign("Radio" & $ar, GUICtrlCreateRadio($aArray[$ar], 10, $top, 350, 20))
            ;MsgBox(0,"",$aArray[$ar])
            $top += 30
            Next
            
             While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg

....

Big thank you for the help. Now i understand the GUI handling better and also the to work with arrays.

Now i build the code in my main script and test it.

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...