Jump to content

Check / Uncheck Menuitems


Recommended Posts

Hi all guys,

I created several Menuitems inside a menu:

Global $CB_Curr[UBound($aCurr)]
$idFileMenu = GUICtrlCreateMenu("CryptoWatcher")
For $i = 1 to UBound($aCurr)-1
    $CB_Curr[$i] = GUICtrlCreateMenuItem($aCurr[$i],$idFileMenu)
Next

I would like to check/uncheck some of them (and save in my settings.ini) so that I can show a Tab that has info only for selected items.

Since I don't have a defined menuitem name but an array, how can I manage it? Through "case ..." seems not possible.

Thanks a lot,

Marco

 

Link to comment
Share on other sites

I made a demo script once that used an INI file to create a checkbox list and saved the states which your post made me think of.

I did a quick and dirty tailor to make it work for a menu instead of CheckBox controls; try it out if you please:

Spoiler
#include <GUIConstantsEx.au3>
#include <Crypt.au3>

Opt("GUIOnEventMode", 1)
Global Const $sINIFile = "test.ini"
Global $aCheckboxes
Global $idFileMenu

GenGUI()

Func GenGUI()
    Local $ManualActionsGui = GUICreate("Test GUI", 200, 300, -1, -1)
    GUICtrlSetFont(-1, 8.5, 700, 0)

    Local $iX = 10, $iY = 10
    $idFileMenu = GUICtrlCreateMenu("CryptoWatcher")

    $aCheckboxes = _ReadINI()
    For $x = 0 To UBound($aCheckboxes) - 1
        Local $idCheckBox = _CreateCheckbox($aCheckboxes[$x][0])
        If _Crypt_HashData($aCheckboxes[$x][0] & "|" & $sINIFile & "|" & $GUI_CHECKED, $CALG_MD5) = $aCheckboxes[$x][1] Then
            GUICtrlSetState(-1, $GUI_CHECKED)
        EndIf
        $aCheckboxes[$x][0] = $idCheckBox
    Next

    GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit", $ManualActionsGui)
    GUISetState()

    While 1
        Sleep(10)
    WEnd
EndFunc   ;==>GenGUI

Func _Checked()
    If BitAnd(GUICtrlRead(@GUI_CtrlId),$GUI_CHECKED) <> $GUI_CHECKED Then
        GUICtrlSetState(@GUI_CtrlId, $GUI_CHECKED)
    Else
        GUICtrlSetState(@GUI_CtrlId, $GUI_UNCHECKED)
    EndIf
EndFunc   ;==>_Checked

Func _CreateCheckbox($sLabel)
    Local $idCheckBox = GUICtrlCreateMenuItem($sLabel,$idFileMenu)
    GUICtrlSetOnEvent(-1, "_Checked")

    Return $idCheckBox
EndFunc   ;==>_CreateCheckbox

Func _Exit()
    _WriteINI()
    Exit
EndFunc   ;==>_Exit

Func _GenINIFile()
    Local $aCheckboxes[10] = ["1st Checkbox", "2nd Checkbox", "3rd Checkbox", "4th Checkbox", "5th Checkbox", "6th Checkbox", "7th Checkbox", "8th Checkbox", "9th Checkbox", "10th Checkbox"]
    FileClose(FileOpen($sINIFile, 2))
    For $x = 0 To UBound($aCheckboxes) - 1
        IniWrite($sINIFile, "Checkbox_Labels", $x, $aCheckboxes[$x])
        IniWrite($sINIFile, "Checkbox_States", $x, "")
    Next
EndFunc   ;==>_GenINIFile

Func _ReadINI()
    If Not FileExists($sINIFile) Then _GenINIFile()
    Local $aCheckbox_Labels = IniReadSection($sINIFile, "Checkbox_Labels")
    If @error Then Return SetError(1, 0, 0) ;If INIReadSection Failed Set Error = 1
    Local $aCheckbox_States = IniReadSection($sINIFile, "Checkbox_States")
    If @error Then Return SetError(2, 0, 0) ;If INIReadSection Failed Set Error = 2

    Local $aCheckboxes[$aCheckbox_Labels[0][0]][2]
    For $x = 0 To ($aCheckbox_Labels[0][0] - 1)
        $aCheckboxes[$x][0] = $aCheckbox_Labels[$x + 1][1]
        $aCheckboxes[$x][1] = $aCheckbox_States[$x + 1][1]
    Next

    Return $aCheckboxes
EndFunc   ;==>_ReadINI

Func _WriteINI()
    For $x = 0 To UBound($aCheckboxes) - 1
        IniWrite($sINIFile, "Checkbox_Labels", $x, GUICtrlRead($aCheckboxes[$x][0], 1))
        IniWrite($sINIFile, "Checkbox_States", $x, _Crypt_HashData(GUICtrlRead($aCheckboxes[$x][0], 1) & "|" & $sINIFile & "|" & BitAND(GUICtrlRead($aCheckboxes[$x][0]),$GUI_CHECKED), $CALG_MD5))
    Next
EndFunc   ;==>_WriteINI

 

Edited by spudw2k
cleaned up tailored demo a little
Link to comment
Share on other sites

Here is an example that is not "on event" mode.  
I didn't bother reading or writing to an ini file.

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <StaticConstants.au3>
#include <Array.au3>

Example()


Func Example()
    GUICreate("My GUI menu", 300, 300)
    Local $sStatus = "Ready"
    Local $aCurr = [0, 1, 0, 1, 1, 0, 1, 0, 1, 1] ; 0 - checkbox unchecked, 1 - checkbox checked.
    Global $CB_Curr[UBound($aCurr)]
    $idFileMenu = GUICtrlCreateMenu("CryptoWatcher")
    For $i = 0 To UBound($aCurr) - 1
        $CB_Curr[$i] = GUICtrlCreateMenuItem("Check " & $i, $idFileMenu)
        ;ConsoleWrite($CB_Curr[$i] & @CRLF) ; Note the id numbers that were created
        If $aCurr[$i] Then
            GUICtrlSetState(-1, $GUI_CHECKED)
        EndIf
    Next

    Local $idBut = GUICtrlCreateButton("Display $aCurr Array", 50, 230, 130, 20)
    Local $idStatusLabel = GUICtrlCreateLabel($sStatus, 0, 265, 300, 16, BitOR($SS_SIMPLE, $SS_SUNKEN))

    GUISetState(@SW_SHOW)

    ; Loop until the user exits.
    While 1
        $Msg = GUIGetMsg()
        Switch $Msg
            Case $GUI_EVENT_CLOSE
                Exit

            Case $CB_Curr[0] To $CB_Curr[UBound($aCurr) - 1]
                $iIndex = $Msg - $CB_Curr[0] ; Finds the index of $CB_Curr array (the Menu Item that was pressed)
                If BitAND(GUICtrlRead($CB_Curr[$iIndex]), $GUI_CHECKED) = $GUI_CHECKED Then
                    GUICtrlSetState($CB_Curr[$iIndex], $GUI_UNCHECKED)
                    GUICtrlSetState($idStatusLabel, $GUI_HIDE)
                    $aCurr[$iIndex] = 0 ; Update $aCurr
                Else
                    GUICtrlSetState($CB_Curr[$iIndex], $GUI_CHECKED)
                    GUICtrlSetState($idStatusLabel, $GUI_SHOW)
                    $aCurr[$iIndex] = 1 ; Update $aCurr
                EndIf
                
            Case $idBut
                _ArrayDisplay($aCurr)
        EndSwitch
    WEnd
EndFunc   ;==>Example

 

Link to comment
Share on other sites

Thanks @spudw2k and @Malkey!

Honestly i'd rather not using checkboxes (they need to be fitted in  MainGui or another Gui. 

And I found so interesting that

3 hours ago, Malkey said:

Case $CB_Curr[0] To $CB_Curr[UBound($aCurr) - 1]

didn't know I could cycle trough them in  Case statement.

In this way I will populate a ListView containing only "checked" menuitems.

I still need to write this code but I'll do that today,

thanks again,

Marco

Link to comment
Share on other sites

Keep in mind, that case statement works because the control IDs are contiguous.  If they were not created one after another then that case statement wouldn't work.  

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