Jump to content

Determine which radio button is pressed


airwilf
 Share

Recommended Posts

Hi

The below script is the gui im having the issue with. At the moment my script will create a different radio button for whatever the value of $x is. This part works fine, the problem is determining which button is being pressed. As the amount of radio buttons is a variable i cant use $radio1, $radio2 so i am struggling to find the answer.

#include <GUIConstantsEx.au3>
$Form1 = GUICreate("Form1", 615, 438, 192, 124)
$Label1 = GUICtrlCreateLabel("Please choose the drive you would like to back your data upto", 48, 16, 520, 28)
GUICtrlSetFont(-1, 16, 400, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
$x = 0
Dim $start = 32
do
$x = $x +1

GUICtrlCreateRadio($x, 48, $start + $start * $x, 300, 33)

until $x = 5
While 1
$msg = GUIGetMsg()
Select
  Case $msg = $GUI_EVENT_CLOSE
  ExitLoop
EndSelect
WEnd
Link to comment
Share on other sites

To determine the state of the radio you use: GUICtrlRead($ControlId)

If a radio button is empty the value returned by the function will be "4" otherwise if it is checked it will be "1".

Link to comment
Share on other sites

Aipion

How do i determine the controlid of the buttons?

As i dont have

$radio1 = GUICtrlCreateRadio("button1", 48, 100, 300, 33)

$radio2 = GUICtrlCreateRadio("button2", 48, 200, 300, 33)

and so on.

Could you show me an example of using what you have suggested within my script?

Thanks

Link to comment
Share on other sites

Please check the helpfile for GUICtrlCreateRadio. There you'll find an example that should explain everything.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19Ā - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13Ā - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28Ā - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
ExcelĀ - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

Ā 

Link to comment
Share on other sites

Example:

#include <GUIConstantsEx.au3>
$Form1 = GUICreate("Form1", 615, 438, 192, 124)
$Label1 = GUICtrlCreateLabel("Please choose the drive you would like to back your data upto", 48, 16, 520, 28)
GUICtrlSetFont(-1, 16, 400, 0, "MS Sans Serif")
$Btn1 = GUICtrlCreateButton("Next",500,400,70,25)
$x = 0
Dim $start = 32
Local $Array[5]
Do
   $x += 1
   $Array[$x-1] = GUICtrlCreateRadio($x, 48, $start + $start * $x, 300, 33)
Until $x = 5
GUISetState(@SW_SHOW)
While 1
   Switch GUIGetMsg()
   Case $GUI_EVENT_CLOSE
   ExitLoop
   Case $Btn1
   $SimText = "Radio Button 1: " & GUICtrlRead($Array[0]) & @CRLF
   $SimText &="Radio Button 2: " &  GUICtrlRead($Array[1]) & @CRLF
   $SimText &="Radio Button 3: " &  GUICtrlRead($Array[2]) & @CRLF
   $SimText &="Radio Button 4: " &  GUICtrlRead($Array[3]) & @CRLF
   $SimText &="Radio Button 5: " &  GUICtrlRead($Array[4])
   MsgBox(0,"States",$SimText)
   EndSwitch
WEnd
Edited by Guest
Link to comment
Share on other sites

Water

I have had a look at the helpfile but this does not give me the answer i need,in the help from what i can see each radio button is statically assigned $radio1 ="", $radio2 = "", when done this way i can see how to check what button is being pressed. However my script is in a loop and creates buttons depending on a variable value, because i dont have $radio1 etc to enter in to GUICtrlRead i am struggling to determine what is being pressed.

Thanks

Link to comment
Share on other sites

  • Moderators

aitwilf,

You need to learn about arrays - then you can change the number of radios very easily with just the one variable:

#include <GUIConstantsEx.au3>

; Set the number of radios here
Global $iRadio_Count = 5

Global $aRadio_IDs[$iRadio_Count + 1], $iStart_Y = 32

$hGUI = GUICreate("Test", 500, 500)

GUICtrlCreateLabel("Please choose the drive to which you would like to backup your data", 50, 50, 500, 30)

$hButton = GUICtrlCreateButton("Next", 400, 400, 70, 25)

For $i = 1 To $iRadio_Count
    $aRadio_IDs[$i] = GUICtrlCreateRadio($i, 50, $iStart_Y * ($i + 1), 300, 30)
Next

GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $hButton
            $sResult = ""
            For $i = 1 To $iRadio_Count
                If GUICtrlRead($aRadio_IDs[$i]) = 1 Then
                    MsgBox(0, "You selected: ", "Radio: " & $i)
                    ExitLoop
                EndIf
            Next
    EndSwitch
WEnd

If you are not too familiar with arrays, then I recommend the Arrays tutorial in the Wiki. ;)

Please ask if you have any questions. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.pngĀ AnyĀ of myĀ own codeĀ posted anywhere on the forumĀ isĀ available for use by others without any restrictionĀ of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSortĀ ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- AĀ highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort,Ā edit and colourĀ ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

Ā 

Link to comment
Share on other sites

Aipion

Thanks for that im going to tweak your code to get what im after. Just out of interest you stated earlier "If a radio button is empty the value returned by the function will be "4" otherwise if it is checked it will be "1"." is there a list that i can see exactly what values mean what? I have looked on the help but jsut seem to be going around in circles.

Thanks Water and Melba23 for your advice

Link to comment
Share on other sites

You will find the list by going to the GUICtrlSetState function on the help file then below that page there will be a list called StateTable

On that list will be Variables you use them instead of numbers.

And By the way there is also a function called GUICtrlGetState which the above list is for, but the GUICtrlRead($ControlId) function also uses some of them.

Edited by Guest
Link to comment
Share on other sites

aitwilf,

You need to learn about arrays - then you can change the number of radios very easily with just the one variable:

#include <GUIConstantsEx.au3>

; Set the number of radios here
Global $iRadio_Count = 5

Global $aRadio_IDs[$iRadio_Count + 1], $iStart_Y = 32

$hGUI = GUICreate("Test", 500, 500)

GUICtrlCreateLabel("Please choose the drive to which you would like to backup your data", 50, 50, 500, 30)

$hButton = GUICtrlCreateButton("Next", 400, 400, 70, 25)

For $i = 1 To $iRadio_Count
    $aRadio_IDs[$i] = GUICtrlCreateRadio($i, 50, $iStart_Y * ($i + 1), 300, 30)
Next

GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $hButton
            $sResult = ""
            For $i = 1 To $iRadio_Count
                If GUICtrlRead($aRadio_IDs[$i]) = 1 Then
                    MsgBox(0, "You selected: ", "Radio: " & $i)
                    ExitLoop
                EndIf
            Next
    EndSwitch
WEnd

If you are not too familiar with arrays, then I recommend the Arrays tutorial in the Wiki. ;)

Please ask if you have any questions. :)

M23

nice, I had the same problem with combobox, I used a bunch of "if's"

thanks for the tip ;)

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there'sĀ InetReadĀ and WinHTTP, way better
happy.png

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