Sign in to follow this  
Followers 0
Fossil Rock

Odd behaviour

9 posts in this topic

I'm trying to detect when a radio button changes state and run a function. The function only needs to run once when the state changes.

The example below says the state has changed when in fact it has not. What have I done wrong?

#include <GUIConstants.au3>

$Form1 = GUICreate("Form1", 155, 108, -1, 300)
$Radio1 = GUICtrlCreateRadio("Option 1", 24, 24, 113, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$Radio2 = GUICtrlCreateRadio("Option 2", 24, 48, 113, 17)
$label = GUICtrlCreateLabel("", 24, 78, 113, 17)
GUISetState(@SW_SHOW)
$state = GUICtrlRead($Radio1)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $state <> GUICtrlRead($Radio1)
            GUICtrlSetData($label, $state & " && " & GUICtrlRead($Radio1))
            _Do_Something()
        
    EndSwitch
$state = GUICtrlRead($Radio1)

WEnd

Func _Do_Something()
    MsgBox(0," Did it really change ??","Radio Button state has changed")
EndFunc

Agreement is not necessary - thinking for one's self is!

My-Colors.jpg

cuniform2.gif

Share this post


Link to post
Share on other sites



Works fine this way, but I would rather not have a bunch of If..Then statements.

#include <GUIConstants.au3>

$Form1 = GUICreate("Form1", 155, 108, -1, 300)
$Radio1 = GUICtrlCreateRadio("Option 1", 24, 24, 113, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$Radio2 = GUICtrlCreateRadio("Option 2", 24, 48, 113, 17)
$label = GUICtrlCreateLabel("", 24, 78, 113, 17)
GUISetState(@SW_SHOW)
$state = GUICtrlRead($Radio1)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        
    EndSwitch

If $state <> GUICtrlRead($Radio1) Then
    GUICtrlSetData($label, $state & " && " & GUICtrlRead($Radio1))
    _Do_Something()
EndIf
$state = GUICtrlRead($Radio1)

WEnd

Func _Do_Something()
    MsgBox(0," Did it really change ??","Radio Button state has changed")
EndFunc

Agreement is not necessary - thinking for one's self is!

My-Colors.jpg

cuniform2.gif

Share this post


Link to post
Share on other sites

You could look at RegisterMsg... but that would be more lines of code than you are doing there.

You could also switch (<< pun ) from Switch to Select and make the Case statement conditional.


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

You could look at RegisterMsg... but that would be more lines of code than you are doing there.

You could also switch (<< pun ) from Switch to Select and make the Case statement conditional.

Sorry, but I can't find anything on 'RegisterMsg'. Got a link or example?

Agreement is not necessary - thinking for one's self is!

My-Colors.jpg

cuniform2.gif

Share this post


Link to post
Share on other sites

Sorry, but I can't find anything on 'RegisterMsg'. Got a link or example?

GUIRegisterMsg()... I was only shortening the function assuming you had used or seen the function before.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

GUIRegisterMsg()... I was only shortening the function assuming you had used or seen the function before.

Yep that one's new to me. I'll see if I can come up with something - Thanks.

Agreement is not necessary - thinking for one's self is!

My-Colors.jpg

cuniform2.gif

Share this post


Link to post
Share on other sites

You could also switch (<< pun ) from Switch to Select and make the Case statement conditional.

That gets my vote!

#include <GUIConstants.au3>

$Form1 = GUICreate("Form1", 155, 108, -1, 300)
$Radio1 = GUICtrlCreateRadio("Option 1", 24, 24, 113, 17)
GUICtrlSetState(-1, $GUI_CHECKED)

$Radio2 = GUICtrlCreateRadio("Option 2", 24, 48, 113, 17)
$label = GUICtrlCreateLabel("", 24, 78, 113, 17)
GUISetState(@SW_SHOW)
$state = GUICtrlRead($Radio1)

While 1
    $nMsg = GUIGetMsg()
    Select
        Case $nMsg = $GUI_EVENT_CLOSE
            Exit
        Case $state <> GUICtrlRead($Radio1)
            GUICtrlSetData($label, $state & " && " & GUICtrlRead($Radio1))
            _Do_Something()
    EndSelect
    $state = GUICtrlRead($Radio1)
WEnd

Func _Do_Something()
    MsgBox(0, " Did it really change ??", "Radio Button state has changed")
EndFunc   ;==>_Do_Something

You can't use Switch as you have it in your first post because it's evaluating whether or not $nMsg is equal to something and the implied structure of

If $nMsg = $state <> GUICtrlRead($Radio1)

is just wrong.

Share this post


Link to post
Share on other sites

You can't use Switch as you have it in your first post because it's evaluating whether or not $nMsg is equal to something and the implied structure of

If $nMsg = $state <> GUICtrlRead($Radio1)

is just wrong.
Ah... now that makes sense.

New code works fine. I took SmoKe_N's advice and decided to select (<< more pun) the Select ... Case method.

Thanks guys. :)

#include <GUIConstants.au3>

$Form1 = GUICreate("Form1", 155, 108, -1, 300)
$Radio1 = GUICtrlCreateRadio("Option 1", 24, 24, 113, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$Radio2 = GUICtrlCreateRadio("Option 2", 24, 48, 113, 17)
$label = GUICtrlCreateLabel("", 24, 78, 113, 17)
GUISetState(@SW_SHOW)
$state = GUICtrlRead($Radio1)
GUICtrlSetData($label, $state & " - " & GUICtrlRead($Radio1))
While 1
    $nMsg = GUIGetMsg()
    Select
        Case $nMsg = $GUI_EVENT_CLOSE
            Exit
        Case $state <> GUICtrlRead($Radio1)
            _Do_Something()
            $state = GUICtrlRead($Radio1)
            Sleep(500); used just to make changes perceivable
            
    EndSelect
    GUICtrlSetData($label, $state & " - " & GUICtrlRead($Radio1))
WEnd

Func _Do_Something()
    GUICtrlSetData($label, $state & " - " & GUICtrlRead($Radio1))
    Sleep(500); used just to make changes perceivable
EndFunc

Agreement is not necessary - thinking for one's self is!

My-Colors.jpg

cuniform2.gif

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Actually, I'll correct myself:

It isn't so much that the implied If $nMsg = $state <> GUICtrlRead($Radio1) is wrong, as that it's that it's doing exactly what you asked it to do.

When using variables of two different data types, AutoIt will try to compare them as the same type. See the help file section "Language Reference - Datatypes".

So if you put this

Case $state <> GUICtrlRead($Radio1)
        MsgBox(0,"",$nMsg & @CRlf & Number($state <> GUICtrlRead($Radio1)))

in the code from your original post, you will see that it shows "$nMsg = 0" and "$state <> GUICtrlRead($Radio1) = 0".

So If $nMsg = $state <> GUICtrlRead($Radio1)

is If 0 = 0

that of course is true and therefore your function is called

Edited by ResNullius

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