ayatest

closing gui with radio buttons

17 posts in this topic

I can't say that I understand what I do, so here is the issue.

I think I know how to get a value from selected radiobutton, but correct me if I did this wrong (I based on autoit example).

What I don't know (don't understand), is - how to exit the loop/gui using custom "close" button, and continue with the script (I assume with variable declared by selected radio button). I'm getting some errors.

At the end, I sent the variable to nothing important, just for debug purposes.

#include <Constants.au3>
#include <GUIConstantsEx.au3>
 
    GUICreate("Select test type", 300, 120)
    Local $idRadio1 = GUICtrlCreateRadio("Short timing", 10, 10, 120, 20)
    Local $idRadio2 = GUICtrlCreateRadio("Long timing", 10, 40, 120, 20)
    GUICtrlSetState($idRadio1, $GUI_CHECKED)
    Local $my_id = GUICtrlCreateButton("Run test", 10, 80, 85, 25); this is my "close" button

    GUISetState(@SW_SHOW) ; will display an  dialog box

    Local $idMsg
    Local $myselect = 3
    ; Loop until the user exits.
    While 1
        $idMsg = GUIGetMsg()
        Select
; how to exit the loop when my_id button is clicked?
;              ExitLoop
        Case $idMsg = $GUI_EVENT_CLOSE ; how to replace this with "ok" button below radio buttons?
                ExitLoop
            Case $idMsg = $idRadio1 And BitAND(GUICtrlRead($idRadio1), $GUI_CHECKED) = $GUI_CHECKED
                $myselect = 1
            Case $idMsg = $idRadio2 And BitAND(GUICtrlRead($idRadio2), $GUI_CHECKED) = $GUI_CHECKED
                $myselect = 2
        EndSelect
    WEnd

sleep($myselect)

functional testing. multimedia recording. flowstone. mouseclick, hotkeys and modularity in general.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

A little further explanation, please. Do you want the script to close completely when you hit the button, or do you want just the GUI to close and some other action to continue, based on the selected radio button?

Edited by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

Radio buttons are for selecting value that will be assigned for a variable (which is used later in the script).

Regular button below them should close the gui/panel with radio buttons, and script should continue with my variable.

So according to your description - option 2: "the GUI to close and some other action to continue, based on the selected radio button".


functional testing. multimedia recording. flowstone. mouseclick, hotkeys and modularity in general.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Then I would do something like this. Remember that you have to get the state of the control before you delete the GUI, hence the need for the $iChecked var:

#include <Constants.au3>
#include <GUIConstantsEx.au3>

    GUICreate("Select test type", 300, 120)
    Local $idRadio1 = GUICtrlCreateRadio("Short timing", 10, 10, 120, 20)
    Local $idRadio2 = GUICtrlCreateRadio("Long timing", 10, 40, 120, 20)
    GUICtrlSetState($idRadio1, $GUI_CHECKED)
    Local $my_id = GUICtrlCreateButton("Run test", 10, 80, 85, 25); this is my "close" button

    GUISetState(@SW_SHOW) ; will display an  dialog box

    ; Loop until the user exits.
    While 1
    Switch GUIGetMsg()
       Case $GUI_EVENT_CLOSE
                ExitLoop
       Case $my_id
        $iChecked = GUICtrlRead($idRadio1)
        GUIDelete()
        $iChecked = 1 ? _myFunc1() : _myfunc2()
        ExitLoop
    EndSwitch
    WEnd

Func _myFunc1()
    MsgBox(0, "", "Chose radio 1")
EndFunc

Func _myFunc2()
    MsgBox(0, "", "Chose radio 2")
EndFunc
Edited by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

If I understand correctly, the solution refers to two radio buttons situation only?

How to generalize it to more than two radio buttons?


functional testing. multimedia recording. flowstone. mouseclick, hotkeys and modularity in general.

Share this post


Link to post
Share on other sites

Something like this, then:

#include <Constants.au3>
#include <GUIConstantsEx.au3>

    Global $aRButtons[10]
    GUICreate("Select test type", 300, 330)
    $iLeft = 10
    $iTop = 30

    For $x = 0 To 9
        $aRButtons[$x] = GUICtrlCreateRadio("Button " & $x, $iLeft, $iTop * $x, 65, 20)
    Next

    Local $my_id = GUICtrlCreateButton("Run test", 100, 80, 85, 25); this is my "close" button

    GUISetState(@SW_SHOW) ; will display an  dialog box

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
           Case $GUI_EVENT_CLOSE
               ExitLoop
           Case $my_id
               For $i = 0 To 9
                   If GUICtrlRead($aRButtons[$i]) = 1 Then ConsoleWrite("You chose button " & $i & @CRLF)
               Next
        EndSwitch
    WEnd

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

Solution on array looks elegant, but...

In the script above - what should happen when clicking on "Run test" button? (I ask because nothing happens here; following the concept - the gui with radio buttons should close, passing selected variable further).

Another issue I see here, is lack of custom naming of radio selections (but I guess this can be mixed with some sort of independent label set).

Basically there will be only few selections, but usually more than two.


functional testing. multimedia recording. flowstone. mouseclick, hotkeys and modularity in general.

Share this post


Link to post
Share on other sites

First, when you press the button only a ConsoleWrite happens, because that is all that has been coded. I have given you a framework to build your script on. Not knowing what you want to do, I can't code it all for you.

As for custom naming, you are pretty limited. You either do it this way, with less code, or you construct each control manually. The latter gives you the ability to give each control its own variable name, but will obviously take up many more lines in your code. Unfortunately, there is no having it both ways :)


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

ayatest,

 

Another issue I see here, is lack of custom naming of radio selections

I suggest using Enum like this: :)

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

; Give these enums sensible names
Enum $eTitle_0, $eTitle_1, $eTitle_2, $eTitle_3, $eMax

Global $aRadio[$eMax]

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

For $i = 0 To $eMax - 1
    $aRadio[$i] = GUICtrlCreateRadio("Radio " & $i, 10, 10 + ($i * 50), 200, 20)
Next

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $aRadio[$eTitle_0] ; And you can use the sensible name to identify the radio
            MsgBox($MB_SYSTEMMODAL, "Actioned", "Radio 0")
        Case $aRadio[$eTitle_1]
            MsgBox($MB_SYSTEMMODAL, "Actioned", "Radio 1")
        Case $aRadio[$eTitle_2]
            MsgBox($MB_SYSTEMMODAL, "Actioned", "Radio 2")
        Case $aRadio[$eTitle_3]
            MsgBox($MB_SYSTEMMODAL, "Actioned", "Radio 3")
    EndSwitch
WEnd
Now you can identify the radio using the Enum name. ;)

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

First, when you press the button only a ConsoleWrite happens, because that is all that has been coded. I have given you a framework to build your script on. Not knowing what you want to do, I can't code it all for you.

As for custom naming, you are pretty limited. You either do it this way, with less code, or you construct each control manually. The latter gives you the ability to give each control its own variable name, but will obviously take up many more lines in your code. Unfortunately, there is no having it both ways :)

 

Well - I asked on how to wire a simple confirmation button, that closes the gui, and allows the script to proceed with variable, that was associated with selected radio button. From what I can say - the example I used - keeps the variable according to what was checked, and the loop goes infinitely, until something happens that breaks that loop. The only thing I don't know in that autoit based example - is how to wire a click-button into the loop (proper syntax in this particular case, to exit the loop when button is pressed), or - whether this is the right method to include such button on first place (and if not, then how it should looks like). My knowledge on autoit is limited, but my understanding of what I read in help manuals is limited even more; my programming originates from visual dataflow platform, where things are completely different, that's why I'm struggling so much..

I appreciate your shortcuts, which I'm sure are pretty useful in many cases, but what I'd like to understand is - how to make a simple gui panel with multiple radio/checkboxes, each with individual name and associated variable (which is used after the loop), and one clickable button that allows to close the gui, so that the script can proceed with variables associated with final gui selections. At the moment, optimization in terms of lines of code - is less relevant for me. Things I'm building are relatively simple scripts, that either use different timings and/or include different test modules.


functional testing. multimedia recording. flowstone. mouseclick, hotkeys and modularity in general.

Share this post


Link to post
Share on other sites

There you go, perhaps Melba's format is more to what you're looking for.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

...but I'm still without the answer to my question.

Let me simplify.

#include <Constants.au3>
#include <GUIConstantsEx.au3> ; add this one when using gui parts!

    GUICreate("Select test type", 300, 120) ; will create a dialog box that when displayed is centered
    Local $my_id = GUICtrlCreateButton("close", 10, 80, 85, 25)

    GUISetState(@SW_SHOW)

    Local $idMsg
    ; Loop until the user exits.
    While 1
        $idMsg = GUIGetMsg()
        Select
        Case $idMsg = $GUI_EVENT_CLOSE
                ExitLoop
        ; put the "close" button here
 EndSelect
        ; ...or here?
    WEnd

 

And the question is. How to make the "close" button to work like the "x" button in the top/right corner of the gui? At the moment I have a problem with only that part. Or I'm missing something?


functional testing. multimedia recording. flowstone. mouseclick, hotkeys and modularity in general.

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

#include <Constants.au3>
#include <GUIConstantsEx.au3> ; add this one when using gui parts!
    GUICreate("Select test type", 300, 120) ; will create a dialog box that when displayed is centered
    Local $my_id = GUICtrlCreateButton("close", 10, 80, 85, 25)
    GUISetState(@SW_SHOW)
    Local $idMsg
    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
           Case $GUI_EVENT_CLOSE, $my_id
              ExitLoop
        EndSwitch
    WEnd

Edited by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

Thanks!


functional testing. multimedia recording. flowstone. mouseclick, hotkeys and modularity in general.

Share this post


Link to post
Share on other sites

I thought the problem is solved, but I was wrong. I still can either play with clickable buttons or radio buttons. Puff... Ok, I redesigned the concept, to make it clear as possible (I hope so)

At least try to run it, to see what is the question about :-)

#include <Constants.au3>
#include <GUIConstantsEx.au3> ; add this one when using gui parts!
 
Opt("WinTitleMatchMode", 2)
HotKeySet("`", "Terminate")

    GUICreate("Select test type", 300, 150)
    Local $idRadio1 = GUICtrlCreateRadio("selection one of many", 10, 10, 120, 20)
    Local $idRadio2 = GUICtrlCreateRadio("other selection", 10, 40, 120, 20)
    Local $idRadio3 = GUICtrlCreateRadio("and another one", 10, 70, 120, 20)
    GUICtrlSetState($idRadio1, $GUI_CHECKED)
    Local $my_id = GUICtrlCreateButton("Close me, exit the loop and proceed to Msgbox", 10, 110, 250, 25); clickable - shouldn't this be event based or so?

    GUISetState(@SW_SHOW)

    Local $idMsg
    Local $myselect = "one"
    ; Loop until the user exits.
    While 1
        $idMsg = GUIGetMsg()
        Select
            Case $idMsg = $GUI_EVENT_CLOSE
                ExitLoop
            Case $idMsg = $idRadio1 And BitAND(GUICtrlRead($idRadio1), $GUI_CHECKED) = $GUI_CHECKED
                $myselect = "one"
            Case $idMsg = $idRadio2 And BitAND(GUICtrlRead($idRadio2), $GUI_CHECKED) = $GUI_CHECKED
                $myselect = "two"
            Case $idMsg = $idRadio3 And BitAND(GUICtrlRead($idRadio3), $GUI_CHECKED) = $GUI_CHECKED
                $myselect = "three"
                EndSelect        
    WEnd

MsgBox($MB_SYSTEMMODAL, "test", $myselect)
sleep(10000)
    
func Terminate()
    Exit
endfunc

 

As for that clickable button to close the gui and proceed - my concern also is - should not this be handled as event or something like that? Because loop uses timer, so it's a hit-or-miss scenario...

Simplifications like some mentioned above are welcome, but I'd like to make the concept work on first place.


functional testing. multimedia recording. flowstone. mouseclick, hotkeys and modularity in general.

Share this post


Link to post
Share on other sites

Not sure why you're overcomplicating your script so much...

Here's something I made for you:

#include <Constants.au3>
#include <GUIConstantsEx.au3>

Global $myselect

Opt("WinTitleMatchMode", 2)
HotKeySet("`", "Terminate")

$gui = GUICreate("Select test type", 300, 150)
$idRadio1 = GUICtrlCreateRadio("selection one of many", 10, 10, 120, 20)
GUICtrlSetState(-1, $GUI_CHECKED)
$idRadio2 = GUICtrlCreateRadio("other selection", 10, 40, 120, 20)
$idRadio3 = GUICtrlCreateRadio("and another one", 10, 70, 120, 20)
$my_id = GUICtrlCreateButton("Close me, exit the loop and proceed to Msgbox", 10, 110, 250, 25)
GUISetState(@SW_SHOW)

While 1
    $idMsg = GUIGetMsg()
    Switch $idMsg
        Case $GUI_EVENT_CLOSE, $my_id
            If GUICtrlRead($idRadio1) = $GUI_CHECKED Then
                $myselect = "one"
            ElseIf GUICtrlRead($idRadio2) = $GUI_CHECKED Then
                $myselect = "two"
            ElseIf GUICtrlRead($idRadio3) = $GUI_CHECKED Then
                $myselect = "three"
            EndIf
            GUIDelete($gui)
            ExitLoop
    EndSwitch
WEnd

MsgBox($MB_SYSTEMMODAL, "test", $myselect)

Func Terminate()
    Exit
EndFunc

Share this post


Link to post
Share on other sites

I overcomplicated it, because I did not knew how to do it. This is exactly what I wanted to get. Thanks!


functional testing. multimedia recording. flowstone. mouseclick, hotkeys and modularity in general.

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