Jump to content

checkbox and if statements


Recommended Posts

Don't know if this makes any sense, but my goal is to check each checkbox and if it's checked then do the following action. Though when I do the if statement, it doesn't write to file on all of them. I'm sure I might mis-used the if statements. Any help is appreciated

 

    if GUICtrlRead($cb1) = $gui_checked Then
    ;code below
        $sReplcb1 = stringregexpreplace($sFile , "(?s)(?<=//\[offstart\])" , "" & "Not available")
        filewrite(FileOpen($dummies, $FO_READ + $FO_OVERWRITE), $sReplcb1)
Elseif GUICtrlRead($cb1) = $gui_checked Then
    ;code below
        $sReplcb1 = stringregexpreplace($sFile , "(?s)(?<=//\[offstart\])" , "" & "Not available")
        filewrite(FileOpen($dummies, $FO_READ + $FO_OVERWRITE), $sReplcb1)
Else
exit
endif

If I check both boxes, only one codes will be written to file. Shouldn't it check the first if then to the second if because I'm using the elseif?

Msgbox(0, "Hate", "Just hate it when I post a question and find my own answer after a couple tries. But if I don't post the question, I can't seem to resolve it at all.")
Link to comment
Share on other sites

An If statement only actions one comparison, whichever one it finds that matches the test first is actioned, so you can not expect it to do more comparisons after it has found a match.

You should use an And operator to test for all possibilities if you want it to check more than one checkbox at a time.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Try this way:

$bCB1=GUICtrlRead($cb1)
$bCB2=GUICtrlRead($cb2)
If $bCB1 = $gui_checked Then
    ;code below
    $sReplcb1 = StringRegExpReplace($sFile, "(?s)(?<=//\[offstart\])", "" & "Not available")
    FileWrite(FileOpen($dummies, $FO_READ + $FO_OVERWRITE), $sReplcb1)
EndIf
If $bCB2 = $gui_checked Then
    ;code below
    $sReplcb1 = StringRegExpReplace($sFile, "(?s)(?<=//\[offstart\])", "" & "Not available")
    FileWrite(FileOpen($dummies, $FO_READ + $FO_OVERWRITE), $sReplcb1)
EndIf
if Not $bCB1 And Not $bCB2 Then Exit

 

Link to comment
Share on other sites

An If statement only actions one comparison, whichever one it finds that matches the test first is actioned, so you can not expect it to do more comparisons after it has found a match.

You should use an And operator to test for all possibilities if you want it to check more than one checkbox at a time.

how would i apply the and with if statement. I don't see how that would operate if i'm trying to compare more than 2 if in the future i want to add more.

 

 

edit: i think i have an idea. Instead, of looking for checked, why don't i look for not checked... 

Edited by asianqueen

Msgbox(0, "Hate", "Just hate it when I post a question and find my own answer after a couple tries. But if I don't post the question, I can't seem to resolve it at all.")
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...