Jump to content

Check State of dynamicly created checkboxes


Go to solution Solved by Subz,

Recommended Posts

Hey Guys, i am currently trying to code a little program that connects certain nas drives to the user based on what checkboxes the user checks. This is necessary so that the users also get connected to the drives when connected to the network via vpn.

The program works by first reading all currently connected drives while the user is connected normally. This means that i have to generate the Checkboxes dynamicly because not all users have access to the same amount of drives and there are also department specific drives.

I currently write all drives to a ini file, then make an array out of it with

Local $Drives_Array = IniReadSectionNames("config.ini")

I then go through the array and create all Checkboxes and also already check them if they were checked before

For $i = 1 to $Drives_Array[0]

      $Drive = $Drives_Array[$i] ; Because the Variable gets redefined below

      $Name = IniRead("config.ini", $Drives_Array[$i], "Name", "Default Value")

      $Drives_Array[$i] = GUICtrlCreateCheckbox($Name, 0, ($i * 35), 140, 30)
      GUICtrlSetFont($Drives_Array[$i], 20, 0, 0, "Calibri")
      GUICtrlSetColor($Drives_Array[$i], 0x225588)

      If IniRead("saveddrives.ini", "Drives", $Drive, "Default Value") == "Connect" Then
         GUICtrlSetState($Drives_Array[$i], $GUI_CHECKED)
      EndIf

      ConsoleWrite($Drives_Array[$i])

   Next

I am now trying to read the state of  the checkboxes when the User presses the "connect" button but i cant get the hang of it...

The idea is to write it to the "saveddrives.ini" and then connect them with another function that already works so that it can be checked if it was connected when the program gets used the next time.

If i now try to get the state of it with

Func Save_Drives()

FileDelete("saveddrives.ini")
_FileCreate("saveddrives.ini")
FileWriteLine("saveddrives.ini", "[Drives]")

Local $Drives_Array_2 = IniReadSectionNames("config.ini")
For $i = 1 to $Drives_Array_2[0]

   If GUICtrlRead($Drives_Array_2[$i]) = $GUI_CHECKED Then
      FileWriteLine("saveddrives.ini", $Drives_Array_2[$i] & "=Connect")
   Else
      FileWriteLine("saveddrives.ini", $Drives_Array_2[$i] & "=Disconnect")
   EndIf

Next

EndFunc

it shows all checkboxes as not checked. The reason i use $Drives_Array_2 is because i get problems when making it a global variable above. If i ConsoleWrite $Drives_Array[$i] and $Drives_Array_2[$i] they are the same(i.e. H:)

Thanks for your help in advance.

Link to comment
Share on other sites

  • Solution

You actually overwrite the values of $Drive_Array when you use GuiCtrlCreateCheckBox, here is how I'd do it, although there are otherways:

Create 2d Array for $Drives_Array

Global $Drives_Array = IniReadSectionNames("config.ini")
   ;~ Add Array Column ControlId
   _ArrayColInsert($Drives_Array, 0)
   ;~ Add Array Column Name
   _ArrayColInsert($Drives_Array, 2)
    For $i = 1 to UBound($Drives_Array) - 1
        ;~ $Drives_Array[$i][0] = ControlId
        ;~ $Drives_Array[$i][1] = Drive example H:
        ;~ $Drives_Array[$i][2] = Drive Name
        $Drives_Array[$i][2] = IniRead("config.ini", $Drives_Array[$i][1], "Name", "Default Value")
        $Drives_Array[$i][0] = GUICtrlCreateCheckbox($Drives_Array[$i][2], 0, ($i * 35), 140, 30)
            GUICtrlSetFont($Drives_Array[$i][0], 20, 0, 0, "Calibri")
            GUICtrlSetColor($Drives_Array[$i][0], 0x225588)
        If IniRead("saveddrives.ini", "Drives", $Drives_Array[$i][1], "Default Value") == "Connect" Then
            GUICtrlSetState($Drives_Array[$i][0], $GUI_CHECKED)
        EndIf
   Next

You can then save using the following function:

Func Save_Drives()
    FileDelete("saveddrives.ini")
    ;~ Loop through GuiControlCreateCheckBox ControlIds
    For $i = 1 To UBound($Drives_Array)-1
        If GUICtrlRead($Drives_Array[$i][0]) = $GUI_CHECKED Then
            IniWrite("saveddrives.ini", "Drives", $Drives_Array[$i][1], "Connect")
        Else
            IniWrite("saveddrives.ini", "Drives", $Drives_Array[$i][1], "Disconnect")
        EndIf
    Next
EndFunc

 

Link to comment
Share on other sites

33 minutes ago, Subz said:

You actually overwrite the values of $Drive_Array when you use GuiCtrlCreateCheckBox, here is how I'd do it, although there are otherways:

Create 2d Array for $Drives_Array

Global $Drives_Array = IniReadSectionNames("config.ini")
   ;~ Add Array Column ControlId
   _ArrayColInsert($Drives_Array, 0)
   ;~ Add Array Column Name
   _ArrayColInsert($Drives_Array, 2)
    For $i = 1 to UBound($Drives_Array) - 1
        ;~ $Drives_Array[$i][0] = ControlId
        ;~ $Drives_Array[$i][1] = Drive example H:
        ;~ $Drives_Array[$i][2] = Drive Name
        $Drives_Array[$i][2] = IniRead("config.ini", $Drives_Array[$i][1], "Name", "Default Value")
        $Drives_Array[$i][0] = GUICtrlCreateCheckbox($Drives_Array[$i][2], 0, ($i * 35), 140, 30)
            GUICtrlSetFont($Drives_Array[$i][0], 20, 0, 0, "Calibri")
            GUICtrlSetColor($Drives_Array[$i][0], 0x225588)
        If IniRead("saveddrives.ini", "Drives", $Drives_Array[$i][1], "Default Value") == "Connect" Then
            GUICtrlSetState($Drives_Array[$i][0], $GUI_CHECKED)
        EndIf
   Next

You can then save using the following function:

Func Save_Drives()
    FileDelete("saveddrives.ini")
    ;~ Loop through GuiControlCreateCheckBox ControlIds
    For $i = 1 To UBound($Drives_Array)-1
        If GUICtrlRead($Drives_Array[$i][0]) = $GUI_CHECKED Then
            IniWrite("saveddrives.ini", "Drives", $Drives_Array[$i][1], "Connect")
        Else
            IniWrite("saveddrives.ini", "Drives", $Drives_Array[$i][1], "Disconnect")
        EndIf
    Next
EndFunc

 

Thank you so much! You helped me out a lot!

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