Jump to content

Check the state of multiple radio buttons.


Nas
 Share

Recommended Posts

@Subz I appreciate your response, but I already know how to check a single radio button, my code has 20 radio buttons, and I want to know if none is checked so the program will get the user an error, but if any of them is checked it runs fine. 

Link to comment
Share on other sites

You still need to go through each variable to check if its checked or not, the easiest way is to use an array for example:

#include <GUIConstantsEx.au3>

Local $aIdSP[20], $bChecked, $idChecked, $y = 10
GUICreate("Example", 190, (UBound($aIdSP) + 1) * 25 + 20)
For $i = 0 To UBound($aIdSP) - 1
    $aIdSP[$i] = GUICtrlCreateRadio("SP0" & $i, 40, $y, 110, 20)
    $y += 25
Next
Local $idSubmit = GUICtrlCreateButton("Submit", 40, $y, 110, 30)

GUISetState()

While 1
    $iMsg = GUIGetMsg()
    Switch $iMsg
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $idSubmit
            $idChecked = ""
            $bChecked = False
            For $i = 0 To UBound($aIdSP) - 1
                If BitAND(GUICtrlRead($aIdSP[$i]), $GUI_CHECKED) = $GUI_CHECKED Then
                    $bChecked = True
                    $idChecked = $aIdSP[$i]
                    ExitLoop
                EndIf
            Next
            If $bChecked Then MsgBox(4096, "Selected", GUICtrlRead($idChecked, 1) & " is selected.")
    EndSwitch
WEnd

 

Link to comment
Share on other sites

If it were me, I'd opt for an event based solution which tracks the last clicked radio button.  Depending on how complex your code is, converting it to OnEventMode instead of using a GUIGetMsg loop may not be cut and dry, but might be worth considering.

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

Local $aIdSP[20], $bChecked, $idChecked, $y = 10
GUICreate("Example", 190, (UBound($aIdSP) + 1) * 25 + 20)
For $i = 0 To UBound($aIdSP) - 1
    $aIdSP[$i] = GUICtrlCreateRadio("SP0" & $i, 40, $y, 110, 20)
    GUICtrlSetOnEvent(-1, "_RadioEvent")    ;<=========
    $y += 25
Next
Local $idSubmit = GUICtrlCreateButton("Submit", 40, $y, 110, 30)
GUICtrlSetOnEvent(-1, "_SubmitButtonEvent") ;<=========

GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")    ;<=========

GUISetState()

While 1
;~     $iMsg = GUIGetMsg()
;~     Switch $iMsg
;~         Case $GUI_EVENT_CLOSE
;~             ExitLoop
;~         Case $idSubmit
;~             $idChecked = ""
;~             $bChecked = False
;~             For $i = 0 To UBound($aIdSP) - 1
;~                 If BitAND(GUICtrlRead($aIdSP[$i]), $GUI_CHECKED) = $GUI_CHECKED Then
;~                     $bChecked = True
;~                     $idChecked = $aIdSP[$i]
;~                     ExitLoop
;~                 EndIf
;~             Next
;~             If $bChecked Then MsgBox(4096, "Selected", GUICtrlRead($idChecked, 1) & " is selected.")
;~     EndSwitch
    sleep(10)   ;<=========
WEnd

Func _RadioEvent()
    $idChecked = @GUI_CtrlId
EndFunc

Func _SubmitButtonEvent()
    If $idChecked Then MsgBox(4096, "Selected", GUICtrlRead($idChecked, 1) & " is selected.")
EndFunc

Func _Exit()
    Exit
EndFunc

 

Link to comment
Share on other sites

In case anyone needs to check/uncheck the same radio button in spudw2k's script...

Func _RadioEvent()
    If $idChecked <> @GUI_CtrlId Then
        $idChecked = @GUI_CtrlId
    Else
        GUICtrlSetState(@GUI_CtrlId, $GUI_UNCHECKED)
        $idChecked = 0
    EndIf
EndFunc

 

Edited by pixelsearch
Link to comment
Share on other sites

Thanks, guys, I was thinking there's a grouping variable that I can check with, but my code is really huge so I end up doing it this way :

Local $Checked = 0
          $Checked = GUICtrlRead($SP01) + GUICtrlRead($SP02) + GUICtrlRead($SP03) + GUICtrlRead($SP05) + GUICtrlRead($SP07) + GUICtrlRead($SP08) +        GUICtrlRead($SP09) + GUICtrlRead($SP10) + GUICtrlRead($SP11)
          $Checked += GUICtrlRead($H03) + GUICtrlRead($H04) + GUICtrlRead($H05) + GUICtrlRead($H06) + GUICtrlRead($H07) + GUICtrlRead($H08) + GUICtrlRead($H09)
          
          if $Checked = 64 then
                MsgBox(16, "Error", "You did not choose any server.")
                EndIf



Edited by Nas
Link to comment
Share on other sites

I would highly suggest throwing some comments in there explaining that explicitly though... that way when you come back in two weeks trying to add another server, you know what 64 means ;)

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

11 hours ago, seadoggie01 said:

I would highly suggest throwing some comments in there explaining that explicitly though.

An even better way would be to assign the value to a well named variable, such as $iServersChksum or some such name, so you know what it refers to, and can be changed at the variable level rather than the condition check level. This way, if you use that value in multiple places as your checksum, then you only have to change it in one place instead of a Find/Replace throughout the script.

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

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