Sign in to follow this  
Followers 0
Kyan

[SOLVED] Contextmenu, change checked when clicked (best way?)

5 posts in this topic

#1 ·  Posted (edited)

Hi everyone :)

There's some way to change checked setting in a context menu without creating a case for every item of context menu?

to read which one os selected I do like this:

Func _ReadItems()
   For $i = 0 to 5 Step 1
        If GUICtrlRead($MenuItem[$i], 1) = $GUI_CHECKED Then
            Return $i
        EndIf
   Next
EndFunc

but for select one I need to create a "case"

Case $MenuItem1
_UnselectAll($Context2)
GUICtrlSetState($MenuItem1, $GUI_CHECKED)

There's some easy way to check items without fill it of "cases"?

Cheers, and thanks for your time ;)

Edited by DiOgO

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



You need to explain a bit more about your problem because what you said or showed so far doesn't make sense to me.

For example,

If GUICtrlRead($MenuItem[$i], 1) = $GUI_CHECKED Then

should be

If GUICtrlRead($MenuItem[$i]) = $GUI_CHECKED Then

I think.

Then you have

$MenuItem[$i]

but later

$MenuItem1

You can toggle the state of a clicked menuitem like this in case it helps

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>

Dim $menuit[4]
GUICreate("My GUI menu", 300, 200)


$filemenu = GUICtrlCreateMenu("&File")

$menuit[0] = GUICtrlCreateMenuItem("Open", $filemenu)

$menuit[1] = GUICtrlCreateMenuItem("Save", $filemenu)

$menuit[2] = GUICtrlCreateMenuItem("Exit", $filemenu)

$menuit[3] = GUICtrlCreateMenuItem("Recent Files", $filemenu)

GUISetState()

While 1
    $msg = GUIGetMsg()


    Switch $msg
        Case -3
            ExitLoop
        Case $menuit[0] To $menuit[3]; then we toggle the checked state
            GUICtrlSetState($msg, $GUI_CHECKED + $GUI_UNCHECKED - BitAND(GUICtrlRead($msg), $GUI_UNCHECKED) - BitAND(GUICtrlRead($msg), $GUI_CHECKED))
    EndSwitch

WEnd
GUIDelete()

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

You need to explain a bit more about your problem because what you said or showed so far doesn't make sense to me.

For example,

If GUICtrlRead($MenuItem[$i], 1) = $GUI_CHECKED Then

should be

If GUICtrlRead($MenuItem[$i]) = $GUI_CHECKED Then

I think.

Then you have

$MenuItem[$i]

but later

$MenuItem1

You can toggle the state of a clicked menuitem like this in case it helps

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>

Dim $menuit[4]
GUICreate("My GUI menu", 300, 200)


$filemenu = GUICtrlCreateMenu("&File")

$menuit[0] = GUICtrlCreateMenuItem("Open", $filemenu)

$menuit[1] = GUICtrlCreateMenuItem("Save", $filemenu)

$menuit[2] = GUICtrlCreateMenuItem("Exit", $filemenu)

$menuit[3] = GUICtrlCreateMenuItem("Recent Files", $filemenu)

GUISetState()

While 1
    $msg = GUIGetMsg()


    Switch $msg
        Case -3
            ExitLoop
        Case $menuit[0] To $menuit[3]; then we toggle the checked state
            GUICtrlSetState($msg, $GUI_CHECKED + $GUI_UNCHECKED - BitAND(GUICtrlRead($msg), $GUI_UNCHECKED) - BitAND(GUICtrlRead($msg), $GUI_CHECKED))
    EndSwitch

WEnd
GUIDelete()

what you done here??

GUICtrlSetState($msg, $GUI_CHECKED + $GUI_UNCHECKED - BitAND(GUICtrlRead($msg), $GUI_UNCHECKED) - BitAND(GUICtrlRead($msg), $GUI_CHECKED))

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

#4 ·  Posted (edited)

what you done here??

GUICtrlSetState($msg, $GUI_CHECKED + $GUI_UNCHECKED - BitAND(GUICtrlRead($msg), $GUI_UNCHECKED) - BitAND(GUICtrlRead($msg), $GUI_CHECKED))

That line just toggles the checked state state of the menu item. I'll explain that in two stages.

To toggle a value between two fixed values you just add the two possible values and take away the current value. So to flip between A and B you say

NewValue = A + B - CurrentValue.

If CurrentValue is A you are left with B, if it's B you are left with A.

The checked state of a menu item is either $GUI_CHECKED or it's $GUI_UNCHECKED.

If the menu item is checked then GuiCtrlRead($menuitem) will guive the state of the item which includes its checked state. So

BitAnd(GuiCtrlRead($MenuItem), $GUI_CHECKED) is either 0 if the item is not checked or it's $GUI_CHECKED. Do the same for testing $GUI_UNCHECKED and that is all I did.

Because we have a case statement and we only get to that line because $msg is one of the menu items, so we can just use $msg because that is set to whichever item was clicked.

I could have made it a little bit shorter by saying

GUICtrlSetState($msg, $GUI_CHECKED + $GUI_UNCHECKED - BitAND(GUICtrlRead($msg), BitOr($GUI_UNCHECKED, $GUI_CHECKED)))

Or you could say

if BitAnd(GuiCtrlRead($msg), $GUI_CHECKED) Then
    $newstate = $GUI_UNCHECKED
Else
    $newstate = $GUI_CHECKED
EndIf

GuiCtrlSetState($msg, $NewState)

Which is easier to read perhaps, in which case it is preferable.

Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

That line just toggles the checked state state of the menu item. I'll explain that in two stages.

To toggle a value between two fixed values you just add the two possible values and take away the current value. So to flip between A and B you say

NewValue = A + B - CurrentValue.

If CurrentValue is A you are left with B, if it's B you are left with A.

The checked state of a menu item is either $GUI_CHECKED or it's $GUI_UNCHECKED.

If the menu item is checked then GuiCtrlRead($menuitem) will guive the state of the item which includes its checked state. So

BitAnd(GuiCtrlRead($MenuItem), $GUI_CHECKED) is either 0 if the item is not checked or it's $GUI_CHECKED. Do the same for testing $GUI_UNCHECKED and that is all I did.

Because we have a case statement and we only get to that line because $msg is one of the menu items, so we can just use $msg because that is set to whichever item was clicked.

I could have made it a little bit shorter by saying

GUICtrlSetState($msg, $GUI_CHECKED + $GUI_UNCHECKED - BitAND(GUICtrlRead($msg), BitOr($GUI_UNCHECKED, $GUI_CHECKED)))

Or you could say

if BitAnd(GuiCtrlRead($msg), $GUI_CHECKED) Then
    $newstate = $GUI_UNCHECKED
Else
    $newstate = $GUI_CHECKED
EndIf

GuiCtrlSetState($msg, $NewState)

Which is easier to read perhaps, in which case it is preferable.

nice way to do the things ;)

thanks a lot ;) it will spare much lines in my script :)

Edited by DiOgO

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