Jump to content

using an array for tray menu items


Recommended Posts

hi

i wonder if anyone can help me with this little problem.

i have an array of tray menu items. by the time the array is created i will not know how many elements it contains as it can vary depending on system configuration. how can i perform case statements on each element in the array but not go past the max index?

Please note: If you plan on submitting any code snippits to my posts, please refrain from using the code tags as i use a screen reader and can't read the code properly, let alone copy it the way it should be.Thanks for your understanding.Best regardsXY16

Link to comment
Share on other sites

hi smartee

ubound does in deed give me the max index, kinda like ArrayMaxIndex does, but i don't know where to start when performing case statements. i have tried using loops, an example is below.

$Array[0] = TrayCreateItem("Item1")

$Array[1] = TrayCreateItem("Item2")

$Array[2] = TrayCreateItem("Item3")

;etc

$Items = UBound($Array, "0")

While 1

$msg = TrayGetMsg()

$Count = "0"

While 1

Select

Case $msg = $Array[$Count]

MsgBox(64, "Info", "Item " & $Count & " was selected.")

if $Count > $Items then ExitLoop

EndSelect

WEnd

WEnd

this doesn't work. i would rather not use an array, but can't figure out how to create a variable named based on a counter, for example $MyVar$Counter.

Any ideas?

Please note: If you plan on submitting any code snippits to my posts, please refrain from using the code tags as i use a screen reader and can't read the code properly, let alone copy it the way it should be.Thanks for your understanding.Best regardsXY16

Link to comment
Share on other sites

  • Moderators

XY16,

This is one way to do it:

#include <Array.au3>

; So we can finally exit

HotKeySet("{ESC}", "On_Exit")

; Create the array

Global $aArray[3]

$aArray[0] = TrayCreateItem("Item1")

$aArray[1] = TrayCreateItem("Item2")

$aArray[2] = TrayCreateItem("Item3")

;etc

; So you can see the values - I get 7, 8, 9

_ArrayDisplay($aArray)

; Strat a timer to simulate a click on the tray

$iBegin = TimerInit()

; Our idle loop

While 1

; Get the event - although here we will never change as there is no change!

$iMsg = TrayGetMsg()

; Simulate a message after 1 Sec - change the value to change the result

If TimerDiff($iBegin) > 1000 Then $iMsg = 8

; Loop through the array to see if the value is within it

For $iCount = 0 To UBound($aArray) - 1

If $iMsg = $aArray[$iCount] Then

; It is!

MsgBox(64, "Info", "Item " & $iCount & " was selected.")

; And exit the For...Next loop - here you will get a continuous firing of the same value as there is no tray to change the value

ExitLoop

EndIf

Next

WEnd

Func On_Exit()

Exit

EndFunc

Please ask if anything is unclear. :unsure:

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

hi

you're example was pretty strait forward, but how can the app tell what item someone clicked?

Please note: If you plan on submitting any code snippits to my posts, please refrain from using the code tags as i use a screen reader and can't read the code properly, let alone copy it the way it should be.Thanks for your understanding.Best regardsXY16

Link to comment
Share on other sites

  • Moderators

XY16,

"how can the app tell what item someone clicked?" - You just use the $iCount variable. :>

To make life easy, I would add another array (or another dimension in the same array as I have shown below) which tells the app what function to run when that particular item is clicked:

#include <Array.au3>

; So we can finally exit

HotKeySet("{ESC}", "On_Exit")

; Create the array

Global $aArray[3][2] ; [0] is ControlID, [1] is function to run

$aArray[0][0] = TrayCreateItem("Item1")

$aArray[0][1] = "Function_0"

$aArray[1][0] = TrayCreateItem("Item2")

$aArray[1][1] = "Function_1"

$aArray[2][0] = TrayCreateItem("Item3")

$aArray[2][1] = "Function_3"

;etc

; So you can see the values - I get 7, 8, 9

_ArrayDisplay($aArray)

; Strat a timer to simulate a click on the tray

$iBegin = TimerInit()

; Our idle loop

While 1

; Get the event - although here we will never change as there is no change!

$iMsg = TrayGetMsg()

; Simulate a message after 1 Sec - change the value to change the result

If TimerDiff($iBegin) > 1000 Then $iMsg = 8

; Loop through the array to see if the value is within it

For $iCount = 0 To UBound($aArray) - 1

If $iMsg = $aArray[$iCount][0] Then

; It is!

Call($aArray[$iCount][1])

; And exit the For...Next loop - here you will get a continuous firing of the same value as there is no tray to change the value

ExitLoop

EndIf

Next

WEnd

Func Function_0()

MsgBox(0, "Hit!", "The user clicked Item 0")

EndFunc

Func Function_1()

MsgBox(0, "Hit!", "The user clicked Item 1")

EndFunc

Func Function_2()

MsgBox(0, "Hit!", "The user clicked Item 2")

EndFunc

Func On_Exit()

Exit

EndFunc

All clear? :unsure:

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

hi

that's loads clearer. i don't think i've been functioning at full capacity this morning due to the fact that i've only just eaten.

thanks allott for your help. that's made my life easier.

Please note: If you plan on submitting any code snippits to my posts, please refrain from using the code tags as i use a screen reader and can't read the code properly, let alone copy it the way it should be.Thanks for your understanding.Best regardsXY16

Link to comment
Share on other sites

to add to my last post, i think it wasn't working properly for me because the TrayAutoPause option is enabled by default, i disabled id it and now everything works fine.

once again thanks for all you're help.

Please note: If you plan on submitting any code snippits to my posts, please refrain from using the code tags as i use a screen reader and can't read the code properly, let alone copy it the way it should be.Thanks for your understanding.Best regardsXY16

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