Jump to content

Radio Button variable value + output -- Help...


aaronkizmet
 Share

Recommended Posts

Hi Guys,

Apologies in advance for the posting of another noob question but I am running into issues when trying to assign variables to selected radio buttons and have them output to an inputbox...

Please see below... I want to select the first radio button, click Calculate button and see the $rbv1 value 10 output to the inputbox field... I am sure it is something very simple I am overlooking but have trawled through many articles on here and cannot seem to come up with the solution...

Thanks in advance

Aaron

#include 
#include 
#include 
#include 
#include 
Opt("GUIOnEventMode", 1)


Local $Adjustment = '', $rb1v = '10', $rbv2 = '20', $RadioValue = ''

$WeatherBKColor = 0x003300
$WeatherFontColor = 0xFFFFFF
$WeatherFontSize = 12
$WeatherFontWeight = 700
$WeatherFontStyle = "Courier New"

GUICreate("Radio Button Read", 600, 400)
GUISetOnEvent ($GUI_EVENT_CLOSE, '_Exit')

GUISetBkColor(0x006633)

GUICtrlCreateGroup("", 8, 12, 287, 247)
$rb1 = GUICtrlCreateRadio("", 190, 45, 20, 20)
GUICtrlSetState(-1, $GUI_CHECKED)
$rb2 = GUICtrlCreateRadio("", 219, 51, 20, 20)
GUICtrlCreateGroup("", -99, -99, 1, 1) ;close group

GUICtrlCreateLabel("Adjustment", 400, 110, 110, 15)
$Adjustment = GUICtrlCreateInput ("", 400, 130, 130, 20)
GUICtrlSetState ($Adjustment, $GUI_DISABLE)


$Calculate = GUICtrlCreateButton("Calculate",450,265,100,50)
GUICtrlSetOnEvent($Calculate,"_Calculate")

GUISetState(@SW_SHOW)


Func _Calculate ()

GUICtrlSetData ($Adjustment, "")
GUICtrlSetData ($RadioValue, "")

Select
Case GUICtrlRead ($rb1, $GUI_CHECKED)
GUICtrlSetData ($RadioValue, $rbv1)
Case GUICtrlRead ($rb2, $GUI_CHECKED)
GUICtrlSetData ($RadioValue, $rbv2)
EndSelect

GUICtrlSetData ($Adjustment, $RadioValue)
GUICtrlSetState ($Adjustment, $GUI_FOCUS)
EndFunc


While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
Exit

EndSwitch
WEnd

Func _Exit ()
Exit
EndFunc
Link to comment
Share on other sites

  • Moderators

aaronkizmet,

Welcome to the AutoIt forum. :)

Here is a modified script that does what you want: ;)

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

Local $Adjustment = '', $rbv1 = '10', $rbv2 = '20', $RadioValue = '' ; Used to read $rb1v !!!!  <<<<<<<<<<<

$WeatherBKColor = 0x003300
$WeatherFontColor = 0xFFFFFF
$WeatherFontSize = 12
$WeatherFontWeight = 700
$WeatherFontStyle = "Courier New"

GUICreate("Radio Button Read", 600, 400)
GUISetOnEvent($GUI_EVENT_CLOSE, '_Exit')

GUISetBkColor(0x006633)

GUICtrlCreateGroup("", 8, 12, 287, 247)
$rb1 = GUICtrlCreateRadio("", 190, 45, 20, 20)
GUICtrlSetState(-1, $GUI_CHECKED)
$rb2 = GUICtrlCreateRadio("", 219, 51, 20, 20)
GUICtrlCreateGroup("", -99, -99, 1, 1) ;close group

GUICtrlCreateLabel("Adjustment", 400, 110, 110, 15)
$Adjustment = GUICtrlCreateInput("", 400, 130, 130, 20)
GUICtrlSetState($Adjustment, $GUI_DISABLE)

$Calculate = GUICtrlCreateButton("Calculate", 450, 265, 100, 50)
GUICtrlSetOnEvent($Calculate, "_Calculate")

GUISetState(@SW_SHOW)

Func _Calculate()

    GUICtrlSetData($Adjustment, "")
    GUICtrlSetData($RadioValue, "")

    Select
        Case GUICtrlRead($rb1) = 1 ; 1 = Checked, 4 = unchecked <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            $RadioValue = $rbv1 ; GUICtrlSetData changes controls - here you just need a simple assignment <<<<<<<<<<<<<<<<<
        Case Else ; Why check the other radio - you only have 2 choices <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            $RadioValue = $rbv2 ; Same simpel assignment here too <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    EndSelect

    GUICtrlSetData($Adjustment, $RadioValue)
    GUICtrlSetState($Adjustment, $GUI_FOCUS)
EndFunc   ;==>_Calculate

#cs  ; What is this doing here - we are in OnEvent mode!!!! <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd
#ce

While 1 ; In OnEvent mode we only need a simple loop to keep the script alive <<<<<<<<<<<<<<<<<<<<<<<<
    Sleep(10) ; This keeps the CPU cool - in MessageLoop mode, GUIGetMsg does this for you automatically <<<<<<<<<<<<
WEnd

Func _Exit()
    Exit
EndFunc   ;==>_Exit

I hope the comments are clear enough - but if not, then please ask. :)

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

Thanks M23... I knew it would be something simple :)

I actually need to have 16 radio buttons in total... so looks good to just use the below as example...

Select
Case GUICtrlRead($rb1) = 1 ; 1 = Checked, 4 = unchecked <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$RadioValue = $rbv1 ; GUICtrlSetData changes controls - here you just need a simple assignment <<<<<<<<<<<<<<<<<
Case GUICtrlRead($rb2) = 1 ; Why check the other radio - you only have 2 choices <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$RadioValue = $rbv2 ; Same simpel assignment here too <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Case GUICtrlRead($rb3) = 1 ; CARRY ON WITH THIS SYNTAX <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$RadioValue = $rbv3 ; Same simpel assignment here too <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
EndSelect
Link to comment
Share on other sites

  • Moderators

aaronkizmet,

so looks good to just use the below as example...

No - very inelegant. Try something using an array along these lines:

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

Local $Adjustment = '', $RadioValue = ''
; Declare an array to hold the CIDs of the radios and values associated
Local $aRadio_Values[16][2]
; Now fill in the values
$aRadio_Values[0][1] = '10'
$aRadio_Values[1][1] = '20'
; etc....
; We will simulate the rest by filling the array in a loop
For $i = 2 To 15
    $aRadio_Values[$i][1] = 10 + ($i * 10)
Next

; And add a variable to hold the index of the last clicked radio
Global $iRadio_Index

$WeatherBKColor = 0x003300
$WeatherFontColor = 0xFFFFFF
$WeatherFontSize = 12
$WeatherFontWeight = 700
$WeatherFontStyle = "Courier New"

GUICreate("Radio Button Read", 600, 400)
GUISetOnEvent($GUI_EVENT_CLOSE, '_Exit')

GUISetBkColor(0x006633)

GUICtrlCreateGroup("", 10, 10, 280, 250)
; Now put the CIDs into the array
For $i = 0 To 3
    For $j = 0 To 3
        Local $iItem = $j + ($i * 4)
        $aRadio_Values[$iItem][0] = GUICtrlCreateRadio("", 20 + ($i * 65), 20 + ($j * 60), 20, 20)
        GUICtrlSetOnEvent(-1, "_Radio_Clicked") ; This will tell us which radio was clicked
    Next
Next
GUICtrlSetState($aRadio_Values[0][0], $GUI_CHECKED)
GUICtrlCreateGroup("", -99, -99, 1, 1) ;close group

GUICtrlCreateLabel("Adjustment", 400, 110, 110, 15)
$Adjustment = GUICtrlCreateInput("", 400, 130, 130, 20)
GUICtrlSetState($Adjustment, $GUI_DISABLE)

$Calculate = GUICtrlCreateButton("Calculate", 450, 265, 100, 50)
GUICtrlSetOnEvent($Calculate, "_Calculate")

GUISetState(@SW_SHOW)

Func _Calculate()

    GUICtrlSetData($Adjustment, "")
    GUICtrlSetData($RadioValue, "")

    ; We know the radio clicked so just read the correct value
    $RadioValue = $aRadio_Values[$iRadio_Index][1]

    GUICtrlSetData($Adjustment, $RadioValue)
    GUICtrlSetState($Adjustment, $GUI_FOCUS)
EndFunc   ;==>_Calculate

While 1
    Sleep(10)
WEnd

Func _Radio_Clicked()

    ; Which radio was clicked?
    $iRadio_Index = @GUI_CtrlId - $aRadio_Values[0][0]
    ; Can you see why this works? <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

EndFunc

Func _Exit()
    Exit
EndFunc   ;==>_Exit

Again 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

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