Jump to content

Switch...Case...Which Radio is Checked?


 Share

Recommended Posts

Question:

How do I check which radio button is checked?

I have 5 radio buttons, the user will select one and press the okay button. I need to pick 1 of 5 functions based on which of the 5 radio buttons are checked. Do I have to make a Case for every possibility? Do I make 1 Case and put in If Statements? What is best?

Thanks in Advance.

*EDIT* I don't understand what the help file means when it says:

So use i.e. BitAnd(GUICtrlRead($Item),$GUI_CHECKED) to test if the control is checked.

#include <GUIConstants.au3>

#Region ### START Koda GUI section ### Form=c:\documents and settings\[secret]\my documents\personal\autoit\koda forms\clearactivity.kxf
$ClearActivity = GUICreate("Clear Activity", 421, 293, 366, 209)
$Label1 = GUICtrlCreateLabel("Clear Activity", 104, 0, 141, 33)
GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif")
$Completed = GUICtrlCreateRadio("Completed", 24, 48, 145, 41)
GUICtrlSetState(-1, $GUI_CHECKED)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$Attempted = GUICtrlCreateRadio("Attempted", 22, 102, 145, 41)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$ReceivedCall = GUICtrlCreateRadio("Received Call", 23, 147, 145, 41)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$LeftMessage = GUICtrlCreateRadio("Left Message", 180, 45, 145, 49)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$Erase = GUICtrlCreateRadio("Erase", 182, 102, 145, 41)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$Button1 = GUICtrlCreateButton("Okay", 48, 232, 129, 49, 0)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$Button2 = GUICtrlCreateButton("Cancel", 192, 232, 153, 49, 0)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$Group1 = GUICtrlCreateGroup("", 8, 32, 337, 185)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        
        Case $Button1

        Case $Button2
            Exit
    EndSwitch
WEnd
Edited by litlmike
Link to comment
Share on other sites

I think if you do individual Cases for $nMsg it will perform the task each time the radio is clicked, rather than only after OK is pressed. You can either do Cases nested after the OK Case or Ifs nested after it.

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
       
        Case $Button1
            If GUICtrlRead($Completed) = $GUI_CHECKED Then
                ...
            ElseIf GUICtrlRead($Attempted) = $GUI_CHECKED Then
                ...
            etc.
            EndIf

        Case $Button2
            Exit
    EndSwitch
WEnd
Edited by ChiDragon
Link to comment
Share on other sites

  • 2 weeks later...

Here's something I've been playing with.... The first radio button doesn't work (haven't figured out why yet).

You can have as many radio buttons as you want ... you might need to work on the GUI if you use too many.

The title changes to show which radio button is selected.

Try changing the $Choices value up and down.

#include <GUIConstants.au3>
$title = "Radio "
$Choices = 10
Dim $Radio[31]
Opt("WinTitleMatchMode", 2)

$GUI = GUICreate("  Radio Button Selector", 170, ($Choices * 20) + 65, -1, -1, "", $WS_EX_TOOLWINDOW)
GUICtrlCreateLabel("Select a Radio Button", 10, 10)
MakeChoices()
GUISetState(@SW_SHOW)
List1()

Func List1()
    $c = 1
While 1
    $msg = GUIGetMsg()
   Select
        Case $msg = $GUI_EVENT_CLOSE
            Done()
        Case GUICtrlRead($Radio[$c]) = $GUI_CHECKED
        WinSetTitle("  Radio Button", "", "  Radio Button #" & $c & " Selected")
    EndSelect
    If $c = $Choices Then $c = 1
    $c = $c + 1
Wend
EndFunc

Func MakeChoices()
For $Counter = 1 To $Choices
    $Radio[$Counter] = GUICtrlCreateRadio($title & $Counter & "  ", 10, ($Counter * 20) + 10)
Next
EndFunc

Func Done()
MsgBox(0,"", "Change the $Choices value and see what happens.")
Exit
EndFunc

Agreement is not necessary - thinking for one's self is!

My-Colors.jpg

cuniform2.gif

Link to comment
Share on other sites

#include <GUIConstants.au3>

#Region ### START Koda GUI section ### Form=c:\documents and settings\[secret]\my documents\personal\autoit\koda forms\clearactivity.kxf
$ClearActivity = GUICreate("Clear Activity", 421, 293, 366, 209)
$Label1 = GUICtrlCreateLabel("Clear Activity", 104, 0, 141, 33)
GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif")
$Completed = GUICtrlCreateRadio("Completed", 24, 48, 145, 41)
GUICtrlSetState(-1, $GUI_CHECKED)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$Attempted = GUICtrlCreateRadio("Attempted", 22, 102, 145, 41)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$ReceivedCall = GUICtrlCreateRadio("Received Call", 23, 147, 145, 41)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$LeftMessage = GUICtrlCreateRadio("Left Message", 180, 45, 145, 49)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$Erase = GUICtrlCreateRadio("Erase", 182, 102, 145, 41)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$Button1 = GUICtrlCreateButton("Okay", 48, 232, 129, 49, 0)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$Button2 = GUICtrlCreateButton("Cancel", 192, 232, 153, 49, 0)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$Group1 = GUICtrlCreateGroup("", 8, 32, 337, 185)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
       
        Case $Button1
            If BitAnd(GUICtrlRead($Completed),$GUI_CHECKED) Then MsgBox(0, "", "Completed")
            If BitAnd(GUICtrlRead($ReceivedCall),$GUI_CHECKED) Then MsgBox(0, "", "Received Call")
        Case $Button2
            Exit
    EndSwitch
WEnd

# MY LOVE FOR YOU... IS LIKE A TRUCK- #
Link to comment
Share on other sites

I modified my earlier code to use events now ... no loop.... and the first radio button works too.

#include <GUIConstants.au3>
Opt("WinTitleMatchMode", 2)
Opt("GUIOnEventMode", 1)

$title = "Option "
$Choices = 5
$Counter = 0
Dim $Radio[31]
$GUI = GUICreate("  Radio Button Selector", 170, ($Choices * 20) + 65, -1, -1, "", $WS_EX_TOOLWINDOW)
GUISetOnEvent($GUI_EVENT_CLOSE, "Done")
GUICtrlCreateLabel("Make a Selection", 10, 10)
MakeChoices()
GUISetState(@SW_SHOW)
List1()

Func List1()
While 1
    $msg = GUIGetMsg()
   Select
        Case $msg = $GUI_EVENT_CLOSE
            Done()
    EndSelect
Wend
EndFunc

Func MakeChoices()
For $Counter = 1 To $Choices
    $Radio[$Counter] = GUICtrlCreateRadio($title & $Counter & "  ", 10, ($Counter * 20) + 10)
    GUICtrlSetOnEvent(-1, "SetTitle")
Next
EndFunc

Func SetTitle()
    WinSetTitle("  ", "", "  " & ControlGetText("","",@GUI_CtrlId) & "Selected")
EndFunc

Func Done()
MsgBox(0,"", "Change the $Choices value and see what happens.")
Exit
EndFunc
Edited by Fossil Rock

Agreement is not necessary - thinking for one's self is!

My-Colors.jpg

cuniform2.gif

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