AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Help with GUI checkbox and loop - can't get it to loop

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
TalksWithComputers



Joined: 16 Aug 2009
Posts: 20

PostPosted: Sun Mar 14, 2010 11:02 am    Post subject: Help with GUI checkbox and loop - can't get it to loop Reply with quote

A script I have switches specific folders in a program on/off, but only one a time with radio buttons. I want to use checkboxes instead so I can a switch on/off all folders with the same script.

If I click the first checkbox, the code executes. With the other two checkboxes it doesn't. It doesn't loop either.

I'm trying to get a part of the script to repeat for each checkbox clicked (Loop).
If the first checkbox is clicked, in the loop the down key should be pressed x times.

What am I missing here?

Thanks!

Code:
SetTitleMatchMode 2

CoordMode, Mouse, Relative

Gui, Add, Checkbox, x36 y7 w90 h30 vCheckbox, &Deutsch
Gui, Add, Checkbox, xp yp+30 wp hp , &English
Gui, Add, Checkbox, xp yp+30 wp hp , &Français
Gui, Add, Button, xp y127 w70 h30 Default gOK, &OK
Gui, Add, Button, xp+80 yp wp hp gCancel, &Abbrechen
Gui, Show, w330 h185, Sprache in PhraseExpress aktivieren
Return

Cancel:
GuiEscape:
GuiClose:
    ExitApp
   
OK:
   Gui, Submit, NoHide
   ToolTip, Du hast Radiobutton %Checkbox% gedrückt!
   Sleep, 400
   ToolTip
   Send ^!p
   WinWaitActive ahk_class TpexMWnd4
   Sleep 1500
   click 100, 100
   Sleep 500
   Send {home} ; geht zum obersten Ordner / Select top folder
   Sleep 200
   Send ^+{left} ; collapse all folders
   Sleep 500
   send {down 2} ; move to "Texte"
   Sleep 400
   Send {right} ; Ordner "Texte" erweitern / expand folder "texte"
   Sleep 400
   send {down}{right} ; Ordner "Sprachen" erweitern / expand folder "sprachen"
   sleep 400
   
   Loop, %checkbox%
   {
   Send {down %Checkbox%} ; Deutsch, English, Français
   sleep 400
   Send +{f10} ; Menutaste / Menu key
   Sleep 400
   send {Down 8} ; Aktiv wählen / Choose state "active"
   Sleep 400
   send {Enter} ; bestätigen / confirm
   Sleep 1000 ; 1 Sek. wartezeit mindestens, weil das Ändern des Zustands von aktiv zu inaktiv lange dauern kann
   send {left}
   Sleep 500
}

   Send !{F4} ; pex schliessen / close PhraseExpress
   Return
Back to top
View user's profile Send private message
ih57452



Joined: 06 Feb 2010
Posts: 57

PostPosted: Sun Mar 14, 2010 12:18 pm    Post subject: Reply with quote

Each checkbox has to have a unique variable. It looks like you had the radio button variable set up to contain 1, 2, or 3 depending on which one was selected. The checkbox variables contain either 0 (not checked) or 1 (checked).

I think this would work:
Code:
SetTitleMatchMode 2

CoordMode, Mouse, Relative

Gui, Add, Checkbox, x36 y7 w90 h30 vCheckbox1, &Deutsch
Gui, Add, Checkbox, xp yp+30 wp hp vCheckbox2, &English
Gui, Add, Checkbox, xp yp+30 wp hp vCheckbox3, &Français
Gui, Add, Button, xp y127 w70 h30 Default gOK, &OK
Gui, Add, Button, xp+80 yp wp hp gCancel, &Abbrechen
Gui, Show, w330 h185, Sprache in PhraseExpress aktivieren
Return

Cancel:
GuiEscape:
GuiClose:
    ExitApp
   
OK:
   Gui, Submit, NoHide
   ToolTip, Du hast Radiobutton %Checkbox% gedrückt!
   Sleep, 400
   ToolTip
   Send ^!p
   WinWaitActive ahk_class TpexMWnd4
   Sleep 1500
   click 100, 100
   Sleep 500
   Send {home} ; geht zum obersten Ordner / Select top folder
   Sleep 200
   Send ^+{left} ; collapse all folders
   Sleep 500
   send {down 2} ; move to "Texte"
   Sleep 400
   Send {right} ; Ordner "Texte" erweitern / expand folder "texte"
   Sleep 400
   send {down}{right} ; Ordner "Sprachen" erweitern / expand folder "sprachen"
   sleep 400
   
   switch(checkbox1)
   switch(checkbox2 * 2)
   switch(checkbox3 * 3)

   Send !{F4} ; pex schliessen / close PhraseExpress
   Return

switch(n) {
  Loop, %n%
   {
   Send {down %n%} ; Deutsch, English, Français
   sleep 400
   Send +{f10} ; Menutaste / Menu key
   Sleep 400
   send {Down 8} ; Aktiv wählen / Choose state "active"
   Sleep 400
   send {Enter} ; bestätigen / confirm
   Sleep 1000 ; 1 Sek. wartezeit mindestens, weil das Ändern des Zustands von aktiv zu inaktiv lange dauern kann
   send {left}
   Sleep 500
   }
   Return
}
Back to top
View user's profile Send private message Send e-mail
TalksWithComputers



Joined: 16 Aug 2009
Posts: 20

PostPosted: Sun Mar 14, 2010 12:46 pm    Post subject: Reply with quote

Thanks! It's much better.
The loop works! Selecting multiple checkboxes does manipulate the correct Folder.

However:
If the 1st checkbox is selected, the script works all right.
If the 2nd checkbox is selected, the script repeats this twice. the script first activates the 2nd folder then deactivates it.
If the 3rd checkbox is selected, the script repeats 3x for the 3rd folder.

Doesn't this line
Code:
Loop, %n%

cause this problem?

Quote:
It looks like you had the radio button variable set up to contain 1, 2, or 3 depending on which one was selected.

Nope.
Original code:
Code:
Gui, Add, Radio, x36 y7 w90 h30 vRadio, &Deutsch
Gui, Add, Radio, xp yp+30 wp hp, &English
Gui, Add, Radio, xp yp+30 wp hp, &Français
Gui, Add, Button, xp y127 w70 h30 Default gOK, &OK
Gui, Add, Button, xp+80 yp wp hp gCancel, &Abbrechen
Gui, Show, w330 h185, Sprache in PhraseExpress aktivieren
Return
Back to top
View user's profile Send private message
ih57452



Joined: 06 Feb 2010
Posts: 57

PostPosted: Sun Mar 14, 2010 1:04 pm    Post subject: Reply with quote

Yeah, if you take out that loop it should fix it. I wasn't sure if that was needed since I couldn't test it.

[EDIT]Actually you'd have to do it a little different without the loop:
Code:

SetTitleMatchMode 2

CoordMode, Mouse, Relative

Gui, Add, Checkbox, x36 y7 w90 h30 vCheckbox1, &Deutsch
Gui, Add, Checkbox, xp yp+30 wp hp vCheckbox2, &English
Gui, Add, Checkbox, xp yp+30 wp hp vCheckbox3, &Français
Gui, Add, Button, xp y127 w70 h30 Default gOK, &OK
Gui, Add, Button, xp+80 yp wp hp gCancel, &Abbrechen
Gui, Show, w330 h185, Sprache in PhraseExpress aktivieren
Return

Cancel:
GuiEscape:
GuiClose:
    ExitApp
   
OK:
   Gui, Submit, NoHide
   ToolTip, Du hast Radiobutton %Checkbox% gedrückt!
   Sleep, 400
   ToolTip
   Send ^!p
   WinWaitActive ahk_class TpexMWnd4
   Sleep 1500
   click 100, 100
   Sleep 500
   Send {home} ; geht zum obersten Ordner / Select top folder
   Sleep 200
   Send ^+{left} ; collapse all folders
   Sleep 500
   send {down 2} ; move to "Texte"
   Sleep 400
   Send {right} ; Ordner "Texte" erweitern / expand folder "texte"
   Sleep 400
   send {down}{right} ; Ordner "Sprachen" erweitern / expand folder "sprachen"
   sleep 400
   
   If (checkbox1)
      switch(checkbox1)
   If (checkbox2)
      switch(checkbox2 * 2)
   If (checkbox3)
      switch(checkbox3 * 3)

   Send !{F4} ; pex schliessen / close PhraseExpress
   Return

switch(n) {
   Send {down %n%} ; Deutsch, English, Français
   sleep 400
   Send +{f10} ; Menutaste / Menu key
   Sleep 400
   send {Down 8} ; Aktiv wählen / Choose state "active"
   Sleep 400
   send {Enter} ; bestätigen / confirm
   Sleep 1000 ; 1 Sek. wartezeit mindestens, weil das Ändern des Zustands von aktiv zu inaktiv lange dauern kann
   send {left}
   Sleep 500
   Return
}


AutoHotkey Help wrote:
The radio button's associated output variable (if any) receives the number 1 for "on" and 0 for "off". However, if only one button in a radio group has a variable, that variable will instead receive the number of the currently selected button: 1 is the first radio button (according to original creation order), 2 is the second, and so on.
Back to top
View user's profile Send private message Send e-mail
TalksWithComputers



Joined: 16 Aug 2009
Posts: 20

PostPosted: Sun Mar 14, 2010 1:40 pm    Post subject: Reply with quote

So great, many thanks! Saved my day, been tinkering with this for months on and off! Very Happy Very Happy Very Happy

Out of curiosity:
What does this mean?
switch(checkbox2 * 2)
Back to top
View user's profile Send private message
ih57452



Joined: 06 Feb 2010
Posts: 57

PostPosted: Sun Mar 14, 2010 2:11 pm    Post subject: Reply with quote

If the checkbox is checked, it's 1 * 2 = 2. If it's not checked it's 0 * 2 = 0. The way I had it written to start with, the loop was acting as an IF statement so that it wouldn't loop (Loop, 0) if the box was not checked. But it looped twice if it was checked, which is not what you wanted, so I had to change it.

It could be more simply written:
Code:
If (checkbox1)
  switch(1)
If (checkbox2)
  switch(2)
If (checkbox3)
  switch(3)


Or you could put an IF in the function to make it still work the way it did to start with:
Code:
switch(checkbox1) ;checked = 1 not checked = 0
switch(checkbox2 * 2) ;checked = 2 not checked = 0
switch(checkbox3 * 3) ;checked = 3 not checked = 0

switch(n) {
  If (n != 0) ;only do something if n is not equal to 0
  {
     ;rest of the function here
  }
  Return
}
Back to top
View user's profile Send private message Send e-mail
TalksWithComputers



Joined: 16 Aug 2009
Posts: 20

PostPosted: Sun Mar 14, 2010 4:16 pm    Post subject: Reply with quote

Great thanks!
I've learned something and hope to write better and more complex scripts in the future!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group