Nas Posted October 15, 2019 Share Posted October 15, 2019 Hello everyone, Is there a way to check if one in multiple radio buttons is checked? $SP02 = GUICtrlCreateRadio("SP02", 40, 10, 113, 17) $SP03 = GUICtrlCreateRadio("SP03", 40, 35, 113, 17) $SP05 = GUICtrlCreateRadio("SP05", 40, 61, 113, 17) $SP07 = GUICtrlCreateRadio("SP07", 40, 86, 113, 17) I just want to check if any of them is checked or not? Link to comment Share on other sites More sharing options...
Subz Posted October 15, 2019 Share Posted October 15, 2019 The GuiCtrlCreateRadio() in the help file has examples on how to check if a radio item is checked. https://www.autoitscript.com/autoit3/docs/functions/GUICtrlCreateRadio.htm Link to comment Share on other sites More sharing options...
Nas Posted October 15, 2019 Author Share Posted October 15, 2019 @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 More sharing options...
Subz Posted October 15, 2019 Share Posted October 15, 2019 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 More sharing options...
spudw2k Posted October 16, 2019 Share Posted October 16, 2019 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. expandcollapse popup#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 pixelsearch 1 Spoiler Things I've Made: Always On Top Tool ◊ AU History ◊ Deck of Cards ◊ HideIt ◊ ICU ◊ Icon Freezer ◊ Ipod Ejector ◊ Junos Configuration Explorer ◊ Link Downloader ◊ MD5 Folder Enumerator ◊ PassGen ◊ Ping Tool ◊ Quick NIC ◊ Read OCR ◊ RemoteIT ◊ SchTasksGui ◊ SpyCam ◊ System Scan Report Tool ◊ System UpTime ◊ Transparency Machine ◊ VMWare ESX BuilderMisc Code Snippets: ADODB Example ◊ CheckHover ◊ Detect SafeMode ◊ DynEnumArray ◊ GetNetStatData ◊ HashArray ◊ IsBetweenDates ◊ Local Admins ◊ Make Choice ◊ Recursive File List ◊ Remove Sizebox Style ◊ Retrieve PNPDeviceID ◊ Retreive SysListView32 Contents ◊ Set IE Homepage ◊ Tickle Expired Password ◊ Transpose ArrayProjects: Drive Space Usage GUI ◊ LEDkIT ◊ Plasma_kIt ◊ Scan Engine Builder ◊ SpeeDBurner ◊ SubnetCalcCool Stuff: AutoItObject UDF ◊ Extract Icon From Proc ◊ GuiCtrlFontRotate ◊ Hex Edit Funcs ◊ Run binary ◊ Service_UDF Link to comment Share on other sites More sharing options...
pixelsearch Posted October 16, 2019 Share Posted October 16, 2019 (edited) 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 October 16, 2019 by pixelsearch Link to comment Share on other sites More sharing options...
Zedna Posted October 16, 2019 Share Posted October 16, 2019 https://www.autoitscript.com/wiki/FAQ#How_can_I_test_if_checkbox_.2F_radiobutton_is_checked.3F Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
Nas Posted October 16, 2019 Author Share Posted October 16, 2019 (edited) 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 October 16, 2019 by Nas pixelsearch 1 Link to comment Share on other sites More sharing options...
pixelsearch Posted October 16, 2019 Share Posted October 16, 2019 Well done Nas. 16 radio buttons x 4 = 64 (as $GUI_UNCHECKED = 4), that works too. Link to comment Share on other sites More sharing options...
seadoggie01 Posted October 17, 2019 Share Posted October 17, 2019 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 functionsAcro.au3 UDF - Automating Acrobat ProToDo Finder - Find #ToDo: lines in your scriptsUI-SimpleWrappers UDF - Use UI Automation more Simply-erKeePass UDF - Automate KeePass, a password managerInputBoxes - Simple Input boxes for various variable types Link to comment Share on other sites More sharing options...
Nas Posted October 17, 2019 Author Share Posted October 17, 2019 @pixelsearch exactly and it works perfectly. @seadoggie01 yes sir , totally forgot about that Link to comment Share on other sites More sharing options...
BrewManNH Posted October 17, 2019 Share Posted October 17, 2019 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. seadoggie01 1 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 GudeHow 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 More sharing options...
Nas Posted October 18, 2019 Author Share Posted October 18, 2019 @BrewManNH Thanks for the advice sir, I will keep that in mind, that variable will only be used once in this script, by checked meant if a radio button is checked or not. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now