Sign in to follow this  
Followers 0
airwilf

Determine which radio button is pressed

10 posts in this topic

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

Share this post


Link to post
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".

Share this post


Link to post
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

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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 Aipion

Share this post


Link to post
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

Share this post


Link to post
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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

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 Aipion

Share this post


Link to post
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

Share this post


Link to post
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
Sign in to follow this  
Followers 0