Jump to content

Radio Buttons -Select-Save


Hobbyist
 Share

Go to solution Solved by Hobbyist,

Recommended Posts

I have 12 radio buttons, one for each month to use for selecting files. I can change the buttons at run time and it saves my choice for the next startup.  I did this by use of creating folders to hold the variables needed at startup - you can this in the functions. Its probably not what advanced users would do, but I am new to this and its the only thing I could think of.  One folder holds the month name, the other holds the number for the "switch" needed at next startup.

;<<<<<<<<<<<<<<<<<<<<<<<<
#include <Array.au3> ;
#include <ButtonConstants.au3>
#include <ColorConstants.au3>
#include <Date.au3>
#include <EditConstants.au3>
#include <File.au3>
#include <GUIConstantsEx.au3>
#include <Misc.au3>
#include <MsgBoxConstants.au3>
;#include <StaticConstants.au3>
#include <String.au3> ;
#include <WindowsConstants.au3>
#include <StringConstants.au3>
#include <FileConstants.au3>
#include <GuiTab.au3>
 #include <GuiButton.au3>
;<<<<<<<<<<<<

$main = GUICreate("Dash Board", 680, 515, 150, 100) ;height was 480
$Group1 = GUICtrlCreateGroup("Default Month", 28, 230, 121, 121)
$Radio1 = GUICtrlCreateRadio("Jan", 38, 242, 50, 17)
$Radio6 = GUICtrlCreateRadio("Jun", 38, 327, 50, 17)
$Radio5 = GUICtrlCreateRadio("May", 38, 310, 50, 17)
$Radio4 = GUICtrlCreateRadio("Apr", 38, 293, 50, 17)
$Radio3 = GUICtrlCreateRadio("Mar", 38, 276, 50, 17)
$Radio2 = GUICtrlCreateRadio("Feb", 38, 259, 50, 17)
$Radio7 = GUICtrlCreateRadio("Jul", 103, 242, 45, 17)
$Radio8 = GUICtrlCreateRadio("Aug", 103, 259, 45, 17)
$Radio9 = GUICtrlCreateRadio("Sept",103, 276, 45, 17)
$Radio10 = GUICtrlCreateRadio("Oct", 103, 293, 45, 17)
$Radio11 = GUICtrlCreateRadio("Nov", 103, 310, 45, 17)
$Radio12 = GUICtrlCreateRadio("Dec", 103, 327, 45, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)

GUISetState(@SW_SHOW)

global $sValue ;variable for switch
_MyCheck () ; read value of switch for check/unchecked at startup

While 1

    Switch GUIGetMsg()


            Case $GUI_EVENT_CLOSE
                Exit

            case $Radio1
             $Monthmgr = "January"
              _MyChoice()
            $sValue = 1
            _MyValue()

            case $Radio2
             $monthmgr = "February"
              _MyChoice()
            $sValue = 2
            _MyValue()

            case $Radio3
             $Monthmgr = "March"
              _MyChoice()
            $sValue = 3
            _MyValue()


            case $Radio4
             $Monthmgr = "April"
              _MyChoice()
            $sValue = 4
            _MyValue()

            case $Radio5
             $Monthmgr = "May"
              _MyChoice()
            $sValue = 5
            _MyValue()


            case $Radio6
             $Monthmgr = "June"
              _MyChoice()
            $sValue = 6
            _MyValue()


            case $Radio7
             $Monthmgr = "July"
              _MyChoice()
            $sValue = 7
            _MyValue()


            case $Radio8
             $Monthmgr = "August"
              _MyChoice()
            $sValue = 8
            _MyValue()

            case $Radio9
             $Monthmgr = "September again"
              _MyChoice()
            $sValue = 9
            _MyValue()

            case $Radio10
             $Monthmgr = "October"
              _MyChoice()
            $sValue = 10
            _MyValue()


            case $Radio11
             $Monthmgr = "November"
              _MyChoice()
            $sValue = 11
            _MyValue()

            case $Radio12
             $Monthmgr = "December"
              _MyChoice()
            $sValue = 12
            _MyValue()

        EndSwitch

    switch  $sValue  ;set new value of switch after startup if desired, show on app

            case 1
            GUICtrlSetState($Radio1,$GUI_checked)

            case 2
            GUICtrlSetState($Radio2,$GUI_checked)

            case 3
            GUICtrlSetState($Radio3,$GUI_checked)

            case 4
            GUICtrlSetState($Radio4,$GUI_checked)

            case 5
            GUICtrlSetState($Radio5,$GUI_checked)

            case 6
            GUICtrlSetState($Radio6,$GUI_checked)

            case 7
            GUICtrlSetState($Radio7,$GUI_checked)

            case 8
            GUICtrlSetState($Radio8,$GUI_checked)

            case 9
            GUICtrlSetState($Radio9,$GUI_checked)

            case 10
            GUICtrlSetState($Radio10,$GUI_checked)

            case 11
            GUICtrlSetState($Radio11,$GUI_checked)

            case 12
            GUICtrlSetState($Radio12,$GUI_checked)


        endSwitch

 WEnd

 Func _MyChoice() ;change month; its saved <<<<<<
      Local $hFile = FileOpen ("c:\Record\2014 File\Default Month\Default.txt", 2)
        FILEWRITE($hFile,  $monthmgr)
        FileClose ($hFile)
    EndFunc

Func _MyValue() ;change value for switch for check/uncheckd  <<<<<<<
    Local $hFile = FileOpen ("c:\Record\2014 File\Default Checked\Checked.txt", 2)
        FILEWRITE($hFile,  $sValue)
        FileClose ($hFile)
EndFunc

Func _MyCheck () ; read value of switch for check/unchecked at startup
    Local $hCheck = FileOpen ("c:\Record\2014 File\Default Checked\Checked.txt", 0)
        $sValue = FileRead ($hCheck )
FileClose ($hCheck )

    EndFunc

Any way it all works but looking at it, I think it could be shortened by use of For/Next or something like that in several ways but everything I have tried is not working.  For instance it would seem I could use a For/Next loop with @mon to get to the name of the month versus all the writing out of the month names (ie radio button "9" use the 9 to get "September")

So can it be shortened?

What would you suggest?

Any help or direction would be appreciated.

Hobbyist

Link to comment
Share on other sites

  • Moderators

Hobbyist,

You could shorten it just a little: :whistle:

#include <GUIConstantsEx.au3>

Global $sIni = @ScriptDir & "\Settings.ini"

Global $aRadio[13]
Global $aMon[13] = ["", "January", "February", "March", "April", "May", "June", _
                    "July", "August", "September", "October", "November", "December"]

$main = GUICreate("Dash Board", 680, 515, 150, 100)

$Group1 = GUICtrlCreateGroup("Default Month", 28, 230, 121, 121)

For $i = 1 To 12
    $iX = (($i > 6) ? (103) : (38))
    $iY = 247 + (17 * Mod($i - 1, 6))
    $aRadio[$i] = GUICtrlCreateRadio(StringLeft($aMon[$i], 3), $iX, $iY, 45, 17)
Next

GUICtrlSetState($aRadio[_MyCheck()], $GUI_CHECKED)

GUISetState()

While 1
    $iMsg = GUIGetMsg()
    Switch $iMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $aRadio[1] To $aRadio[12]
            For $i = 1 To 12
                If $iMsg = $aRadio[$i] Then
                    $Monthmgr = $aMon[$i]
                    _MyChoice($Monthmgr)
                    _MyValue($i)
                    ExitLoop
                EndIf
            Next
    EndSwitch
WEnd

Func _MyChoice($sMon)
    IniWrite($sIni, "Month", "Month", $sMon)
EndFunc   ;==>_MyChoice

Func _MyValue($i)
    IniWrite($sIni, "Checked", "Checked", $i)
EndFunc   ;==>_MyValue

Func _MyCheck()
    Return IniRead($sIni, "Checked", "Checked", 1) ; Default is 1
EndFunc   ;==>_MyCheck
The main changes:

 

- Use of an ini file to store the saved data - that is what they are designed to do.

- Use of arrays to hold the radio ControlIDs and month names - makes it easier to loop through them.

- Use of algorithms to locate each of the radios as they are created.

- Looping through array of radio ControlIDs to see whch was pressed.

Please ask if you have any questions or of any of the code is unclear. :)

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

  • Solution
Thanks Melba!
 
I understand most of it and need a little clarification on some.
 
The For/Next loop for creating the buttons - never saw anything like this before, but gather it has something to do with x/y coordinates - but just what, I am at a loss. Soooo if you could enlighten me, it would be a great learning experience.
 
Next - the Case $aRadio[1] To $aRadio[12], is that the "values" in the array?   I had forgotten about the "value to value" condition and certainly too myopic to think of using an array.
 
Lastly - ini  ????? Never did I think of it, just don't know anything about 'em. Do they store within my app itself or reside somewhere else and get pulled back in.  I know I should and will be reading the help files on this one.
 
Nice, very nice.
Hobbyist
Link to comment
Share on other sites

  • Moderators

Hobbyist,

1. The x/y algorithms are just using the number in the loop variable to calculate the required x/y values. As you have a regular pattern for the buttons, it is not too difficult to devise algorithms which produce the correct values for each radio as you progress through the loop.

The x coord is the easiest to explain as there is only one change to make:

$iX = (($i > 6) ? (103) : (38))
This is a simple ternary function: If $I > 6 then use 103, if not then use 38. It could also be written like this:

If $i > 6 Then
    $iX = 103
Else
    $iX = 38
EndIf
The y coord is a little more complicated as the value must increase and then reset:

$iY = 247 + (17 * Mod($i - 1, 6))
What we have here is a base value (247) with an interval of 17 between each line - for the first 6 radios this is pretty easy to understand. The trick is to use the Mod function to reset the required count for the second column - this returns the integer remainder of a division by a value (here 6) which gives us the following result:

Original value    Value - 1   Mod 6 value
1                 0           0 
2                 1           1
3                 2           2
4                 3           3
5                 4           4
6                 5           5
7                 6           0 (6 + 0)
8                 7           1 (6 + 1)
9                 8           2 (6 + 2)
10                9           3 (6 + 3)
11                10          4 (6 + 4)
12                11          5 (6 + 5)
The Mod return gives you the number of intervals you need to add to the baseline figure to get the y coord.

2. The Switch Case does indeed look for the values of the ControlIDs stored in the array. The trick is that AutoIt allocates ControlIDs as consecutive numbers (they are actually indices for an internal array of AutoIt created controls). I say "trick" because AutoIt actually allocates the lowest available slot in this array, so if you have previously deleted controls you may find the series of the newly created controls are not consecutive. To be absolutely correct I should have used Case Else, but in most cases you can use the To construct without problem.

3. Many people will tell you that ini files are out-dated - I think that for simple data storage of the type you are using here they are a perfect solution. They are small text files with a fairly strict internal structure- and you are quite right in thinking that reading the Help file will explain all about them, so I am not going into detail here. You can store them where you wish - all you need to do is make sure that you place them somewhere without problems for writing - if the main app is in C:Program Files you would be ill-advised to store the ini file in the same folder if you use Vista+, much better to create a specific folder under UserAppDataLocal.

I hope that clears up some of the fog - keep asking if not. :)

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

Melba

I hope this thread isn't too old to revisit......

I'm running into an issue and not sure what to look for.

Your streamlined code works great and makes sense once I studied it.

So I copied it into my code and it works but here is what is happening.

In order for a change to happen (one month to another) I have to click twice. That is the only way I can see a change in the ini file.

So it works but not with a single click as in yours, yet all I did was copy it over to mine.

So any suggestions as to what I need to look for or how to trouble shoot the issue??

Thanks

Hobbyist

Edited by Hobbyist
Link to comment
Share on other sites

I forgot to include the below script.

Can I do this or is it possibly the issue?

While 1
 
 
$iMsg = GUIGetMsg() ;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<  Notice
Switch $iMsg
Case $GUI_EVENT_CLOSE
Exit
Case $aRadio[1] To $aRadio[12]
 
For $i = 1 To 12
GUICtrlSetState($aRadio[$i], $GUI_FOCUS)
If $iMsg = $aRadio[$i] Then
$Monthmgr = $aMon[$i] &" " &StringTrimRight(_NowCalc() ,15)
_MyChoice($Monthmgr)
_MyValue($i)
 
ExitLoop
EndIf
Next
EndSwitch
 
Switch GUIGetMsg() ;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< And Notice This
 
Case $List3
blah
blah blah
Link to comment
Share on other sites

  • Moderators

Hobbyist,

Of course that is the issue - adding another call to GUIGetMsg means that you discard the previous return and so you need a second click to fire anything in the second loop. Just add all the Case structures inside the same loop like this:

While 1

    $iMsg = GUIGetMsg() ;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<  Notice
    Switch $iMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $aRadio[1] To $aRadio[12]
            For $i = 1 To 12
                GUICtrlSetState($aRadio[$i], $GUI_FOCUS)
                If $iMsg = $aRadio[$i] Then
                    $Monthmgr = $aMon[$i] & " " & StringTrimRight(_NowCalc(), 15)
                    _MyChoice($Monthmgr)
                    _MyValue($i)
                    ExitLoop
                EndIf
            Next

        Case $List3
            ; blah
            ; blah blah
            
    EndSwitch
WEnd
Now you should be able to detect all the controls being fired on the first activation. ;)

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