Jump to content

Why my Select All doesn't work?


Recommended Posts

Hello

I''m preparing a GUI with checkboxes pointing to websites (for now just fake addresses for test). From declared array I'm creating GUI with checkboxes names. as you can see i'm using a loop for that. However my loop in Select All button doesn't work. I'm also using same type of loop just like in GUI creation but Select all only selects the last value. Case $ButtonSel. Why?

 

Global $Chrome = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
Local $Arr[3] = ["Movies", "Games", "Books"]
Global $Max = UBound($Arr) - 1

GUICreate("INTERNET SITES", 500, 200, -1, -1)
$Top = 10

For $i = 0 To $Max
      Global $Chkbx = GUICtrlCreateCheckbox($Arr[$i], 10, $Top, 150, 20)
      $Top += 30
Next

$ButtonSel = GUICtrlCreateButton("Select All", 10, 110, 150, 20)
$ButtonOpen = GUICtrlCreateButton("Open", 10, 140, 150, 20)
GUISetState(@SW_SHOW)

Local $ChkbxSel = 0

While 1
  $ChkbxSel = GUIGetMsg()
  Switch $ChkbxSel
   Case $GUI_EVENT_CLOSE
      ExitLoop
   Case $ButtonSel
      For $i = 0 To $Max
         GUICtrlSetState($Chkbx, $GUI_CHECKED)
      Next
   Case $ButtonOpen
      $Srv = GUICtrlRead($ChkbxSel)
      $Addr = " https://www." & $Srv & ".com"
      ;MsgBox($MB_OK,"",$Addr & " selected")
      Run($Chrome & $Addr)
  EndSwitch
WEnd

 

Edited by Ghost1982
Link to comment
Share on other sites

Link to comment
Share on other sites

Here is a snippet related to what you are working on.  It might give you some ideas.  It uses an array to track the controls as Danp2 suggested, but also uses a TreeView control instead which simplifies placement of the CheckBoxes.

 

Link to comment
Share on other sites

10 hours ago, Nine said:

Never declare Global or Local in a loop as this may override the previous assignment. 

I agree we shouldn’t, but what way in a practical sense is

For $i = 0 To $Max
      Global $Chkbx = GUICtrlCreateCheckbox($Arr[$i], 10, $Top, 150, 20)
      $Top += 30
Next

different  than

For $i = 0 To $Max
      $Chkbx = GUICtrlCreateCheckbox($Arr[$i], 10, $Top, 150, 20)
      $Top += 30
Next

and wouldn’t any “override [of] the previous assignment” occur  regardless of whether the code is looped or not?

Code hard, but don’t hard code...

Link to comment
Share on other sites

8 hours ago, spudw2k said:

Here is a snippet related to what you are working on.  It might give you some ideas.  It uses an array to track the controls as Danp2 suggested, but also uses a TreeView control instead which simplifies placement of the CheckBoxes.

 

Looks great,

 

Why are you using 0 + 1?

 

Dim $SelectionArray[$ChoiceArray[0] + 1]

 

For now I've done something like below, The simples as it can be. Also I've added Unselect All button.

 

Global $Chrome = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
$Arr = StringSplit("Movies|Games|Books|Sport|News","|")
Dim $Sel[$Arr[0] + 1]

GUICreate("IBM DataPower Gateway", 500, 400, -1, -1)
$Top = 10

For $i = 1 To $Arr[0]
      $Sel[$i] = GUICtrlCreateCheckbox($Arr[$i], 10, $Top, 150, 20)
      $Top += 25
Next

$ButtonSel = GUICtrlCreateButton("Select All", 10, 150, 150, 20)
$ButtonUnSel = GUICtrlCreateButton("Unselect All", 10, 180, 150, 20)
$ButtonOpen = GUICtrlCreateButton("Open", 10, 210, 150, 20)

GUISetState(@SW_SHOW)

While 1
  $ChkbxSel = GUIGetMsg()
  Switch $ChkbxSel
   Case $GUI_EVENT_CLOSE
      ExitLoop
   Case $ButtonSel
      For $i = 1 To $Arr[0]
         GUICtrlSetState($Sel[$i], $GUI_CHECKED)
      Next
   Case $ButtonUnSel
      For $i = 1 To $Arr[0]
         GUICtrlSetState($Sel[$i], $GUI_UNCHECKED)
      Next
   Case $ButtonOpen
      For $i = 1 To $Arr[0]
         If GUICtrlRead($Sel[$i]) = $GUI_CHECKED Then
            $Addr = " https://www." & $Arr[$i] & ".com"
            ;MsgBox($MB_OK,"",$Addr & " selected")
            Run($Chrome & $Addr)
            ;MsgBox($MB_OK,"",$Arr[$i] & " selected")
         EndIf
      Next
  EndSwitch
WEnd

 

However now I'm thinkg about another problem. The more items I'm adding into array the higher the interface grows. Is there any command that allows to quickly split checkboxes into columns? i.e. 3 columns with max 3 items.

Edited by Ghost1982
Link to comment
Share on other sites

17 hours ago, Zedna said:

Use function Mod() - modulus operation

Isn't it diving by two? Only thing that comes to my mind is to create a some kind of loop where I'll define number of columns and rows. However I'm not sure how should I point in the loop when the row limit (3) is reached. 

Link to comment
Share on other sites

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

Global $Chrome = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
$Arr = StringSplit("Movies|Games|Books|Sport|News|Movies2|Games2|Books2|Sport2|News2|Movies3|Games3|Books3|Sport3","|")
Dim $Sel[$Arr[0] + 1]

GUICreate("IBM DataPower Gateway", 500, 400, -1, -1)
$Left = 10
$Top = 10
$rows = 5 ; number of checkboxes in each column
$TopMax = $Top + $rows*25

For $i = 1 To $Arr[0]
      $Sel[$i] = GUICtrlCreateCheckbox($Arr[$i], $Left, $Top, 150, 20)
      If Mod($i, $rows) = 0 Then
        $Left += 150
        $Top = 10
      Else
        $Top += 25
      EndIf
Next

$ButtonSel = GUICtrlCreateButton("Select All", 10, $TopMax, 150, 20)
$ButtonUnSel = GUICtrlCreateButton("Unselect All", 10, $TopMax + 30, 150, 20)
$ButtonOpen = GUICtrlCreateButton("Open", 10, $TopMax + 60, 150, 20)

GUISetState(@SW_SHOW)

While 1
  $ChkbxSel = GUIGetMsg()
  Switch $ChkbxSel
   Case $GUI_EVENT_CLOSE
      ExitLoop
   Case $ButtonSel
      For $i = 1 To $Arr[0]
         GUICtrlSetState($Sel[$i], $GUI_CHECKED)
      Next
   Case $ButtonUnSel
      For $i = 1 To $Arr[0]
         GUICtrlSetState($Sel[$i], $GUI_UNCHECKED)
      Next
   Case $ButtonOpen
      For $i = 1 To $Arr[0]
         If GUICtrlRead($Sel[$i]) = $GUI_CHECKED Then
            $Addr = " https://www." & $Arr[$i] & ".com"
            ;MsgBox($MB_OK,"",$Addr & " selected")
            Run($Chrome & $Addr)
            ;MsgBox($MB_OK,"",$Arr[$i] & " selected")
         EndIf
      Next
  EndSwitch
WEnd

 

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