Jump to content

Updating menu items on event


Recommended Posts

Hi guys,

I'm using this startup example to allow the program menu to insert an option to "AutoStart" the script. What I'd like to do is update the checkbox of the menu item after they click it to then reflect the "AutoStart" state (on or off).

How do I update the menu on the event someone clicks the menu option? The registry is updating correctly, and if I close and re-run the script the menu is updated, but not in the same script execution. I wasn't sure with TrayItemSetState for my "Func AutoStart()" Function what the "controlID" was. I tried 0 through to 3 and "AutoStart" text. Perhaps this is where I'm going wrong?

; Add custom tray icon menu
#NoTrayIcon
#include <MsgBoxConstants.au3>
#include <TrayConstants.au3>
Opt("TrayMenuMode", 3)
Opt("TrayOnEventMode", 1)

#include '_Startup.au3'
TrayCreateItem("AutoStart")
TrayItemSetOnEvent(-1, "AutoStart")
If _StartupRegistry_Exists() Then
    TrayItemSetState(-1, $TRAY_CHECKED)
Else
    TrayItemSetState(-1, $TRAY_UNCHECKED)
EndIf
TrayCreateItem("About")
TrayItemSetOnEvent(-1, "About")
TrayCreateItem("")
TrayCreateItem("Exit")
TrayItemSetOnEvent(-1, "ExitScript")
TraySetOnEvent($TRAY_EVENT_PRIMARYDOUBLE, "About")
TraySetState($TRAY_ICONSTATE_SHOW)


; ##################
; ### OTHER CODE ###
; ##################


Func About()
    MsgBox(0,"About","etc")
EndFunc

Func AutoStart()
    If _StartupRegistry_Exists() Then
        _StartupRegistry_Uninstall()
        TrayItemSetState("AutoStart", $TRAY_UNCHECKED)
    Else
        _StartupRegistry_Install()
        TrayItemSetState("AutoStart", $TRAY_CHECKED)
    EndIf
EndFunc

Func ExitScript()
    Exit
EndFunc

Thanks!

Link to comment
Share on other sites

I note that I could probably use the "$TRAY_ITEM_RADIO" for "TrayCreateItem" but would prefer the check box, looks cleaner and only gives them 1 option.

The checkbox appears to function, it's just the menu isn't updating while the script is running.

Link to comment
Share on other sites

  • Moderators

WoodGrain,

You need to use a ControlID in the TrayItemSetState call - at the moment you are using a literal text string:

#include <TrayConstants.au3>

Opt("TrayMenuMode", 3)
Opt("TrayOnEventMode", 1)

Global $bRegIstalled = False

$cAutoStart = TrayCreateItem("AutoStart")   ; Store ControlID here
TrayItemSetOnEvent($cAutoStart, "AutoStart")

TrayCreateItem("About")
TrayCreateItem("")
TrayCreateItem("Exit")
TrayItemSetOnEvent(-1, "ExitScript")

TraySetState($TRAY_ICONSTATE_SHOW)

While 1
    Sleep(10)
WEnd



Func AutoStart()
    If $bRegIstalled Then
        $bRegIstalled = False
        TrayItemSetState($cAutoStart, $TRAY_UNCHECKED) ; Use it here...
    Else
        $bRegIstalled = True
        TrayItemSetState($cAutoStart, $TRAY_CHECKED)   ; ...and here
    EndIf

EndFunc



Func ExitScript()
    Exit
EndFunc

All clear?

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

×
×
  • Create New...