D3fr0s7 Posted November 19, 2022 Posted November 19, 2022 I'm trying to make one tray item delete another, but when I do this, all tray items that were created after the deleted item don't work as intended, as if their controlID's were all shifted down one value, and their corresponding tray items now (after deletion) run the code of the tray item before it. Am I missing something? Is there a better way to accomplish what I'm trying to do? expandcollapse popup#include <TrayConstants.au3> #include <Array.au3> HotKeySet ( "{ESC}", "Abort" ) Opt ( "TrayMenuMode", 3 ) TraySetState($TRAY_ICONSTATE_SHOW) ; Show the tray menu. Global $aTray[8] ; Defines array to hold tray items. $aTray[0] = TrayCreateItem ( "Test 1 (Name Test 5)" ) $aTray[1] = TrayCreateItem ( "Test 2 (Delete Test 5)" ) $aTray[2] = TrayCreateItem ( "Test 3 (Restore Test 5)" ) $aTray[3] = TrayCreateItem ( "Test 4 (Check if Test 5 is blank or space)" ) $aTray[4] = TrayCreateItem ( "Test 5 Delete Me" ) $aTray[5] = TrayCreateItem ( "Test 6 (Check Test 5 Text)" ) $aTray[6] = TrayCreateItem ( "Test 7 (Read Values)" ) $aTray[7] = TrayCreateItem ( "Test 8 (Count Blanks)" ) While 1 Switch TrayGetMsg() Case $aTray[0] ; "Test 1" Change Test 5 Text. If TrayItemGetText ( $aTray[0] ) <> "" Then Global $TrayText = InputBox ( "Test", "Choose text for Test 5", "Test 5 Delete Me" ) TrayItemSetText ( $aTray[4], $TrayText) EndIf Case $aTray[1] ; "Test 2" Deletes "Test 5". If TrayItemGetText ( $aTray[1] ) <> "" Then Global $TrayDeletedName = TrayItemGetText ( $aTray[4] ) TrayItemDelete ( $aTray[4] ) _ArrayInsert ( $aTray, 4 ) EndIf Case $aTray[2] ; "Test 3" Restores "Test 5". If TrayItemGetText ( $aTray[2] ) <> "" Then $aTray[4] = TrayCreateItem ( $TrayDeletedName ) EndIf Case $aTray[3] ; "Test 4" Check if Test 5 value is blank, space, or filled. If TrayItemGetText ( $aTray[3] ) <> "" Then If TrayItemGetText ( $aTray[4] ) = "" Then MsgBox ( 0, "Test", "Test 5 is blank" ) ElseIf TrayItemGetText ( $aTray[4] ) = " " Then MsgBox ( 0, "Test", "Test 5 is not blank (space)" ) Else MsgBox ( 0, "Test", "Test 5 is assigned a value" ) EndIf EndIf Case $aTray[4] ; "Test 5" (Item to test for, during, and after deletion). If TrayItemGetText ( $aTray[4] ) <> "" Then MsgBox ( 0, "Test", "I'm here!" ) EndIf Case $aTray[5] ; "Test 6" Displays Text from Test 5 item. If TrayItemGetText ( $aTray[5] ) <> "" Then $Test5Text = TrayItemGetText ( $aTray[4] ) MsgBox ( 0, "Test", "Test 5 Text: " & $Test5Text ) EndIf Case $aTray[6] ; "Test 7" Displays all item values. If TrayItemGetText ( $aTray[6] ) <> "" Then MsgBox ( 0, "Test", "$aTray[0]: " & $aTray[0] & @CRLF & _ "$aTray[1]: " & $aTray[1] & @CRLF & _ "$aTray[2]: " & $aTray[2] & @CRLF & _ "$aTray[3]: " & $aTray[3] & @CRLF & _ "$aTray[4]: " & $aTray[4] & @CRLF & _ "$aTray[5]: " & $aTray[5] & @CRLF & _ "$aTray[6]: " & $aTray[6] & @CRLF & _ "$aTray[7]: " & $aTray[7] & @CRLF ) EndIf Case $aTray[7] ; "Test 8" Counts all blanks in tray values. If TrayItemGetText ( $aTray[7] ) <> "" Then Global $blankCount = _ArrayFindAll ( $aTray, "" ) If $blankCount = -1 Then If @error = 6 Then MsgBox ( 0, "Test", "Error, No blanks present") EndIf Else MsgBox ( 0, "Test", "# of blanks: " & $blankCount ) EndIf EndIf EndSwitch WEnd Func Abort() Exit EndFunc Here is a test script I created to try to troubleshoot the problem on my own, with no luck. pay specific attention to "Test 2" ($aTray[1]), "Test 5" ($aTray[4]), and how every tray item after "Test 5" ($aTray[4]) behaves after deletion. Clicking "Test 2" will delete tray item "Test 5", after deletion every item runs the code of the tray item that was established before it (ex. "Test 3" and "Test 4" run their respective code, "Test 5" no longer exists, "Test 6" runs "Test 7", "Test 7" runs "Test 8"), and the last item ("Test 8" $aTray[7]) has no effect when the tray item is clicked. I understand that deleting the tray item changes the controlID, but I don't know in what way it does, and therefore how I can fix it to be able to achieve what I want it to. I appreciate any help or guidance with this problem. To clarify, what I'm ultimately trying to do is create a 'while' loop with switch case functions that can exist without necessarily being linked to a tray item, so that I can add and delete them at liberty using the script's functions, without having to differentiate switch case functions with if functions (if $aTray[x] exists, then use this set of switch case functions, etc.). Please, I am in pain. Water come school me again pls
Developers Solution Jos Posted November 19, 2022 Developers Solution Posted November 19, 2022 Check if something like this helps you: expandcollapse popup#include <TrayConstants.au3> #include <Array.au3> HotKeySet("{ESC}", "Abort") Opt("TrayMenuMode", 3) TraySetState($TRAY_ICONSTATE_SHOW) ; Show the tray menu. Global $TrayText Global $aTray[8][2] ; Defines array to hold tray items. $aTray[0][0] = "Test 1 (Name Test 5)" $aTray[1][0] = "Test 2 (Delete Test 5)" $aTray[2][0] = "Test 3 (Restore Test 5)" $aTray[3][0] = "Test 4 (Check if Test 5 is blank or space)" $aTray[4][0] = "Test 5 Delete Me" $aTray[5][0] = "Test 6 (Check Test 5 Text)" $aTray[6][0] = "Test 7 (Read Values)" $aTray[7][0] = "Test 8 (Count Blanks)" $aTray[0][1] = TrayCreateItem($aTray[0][0]) $aTray[1][1] = TrayCreateItem($aTray[1][0]) $aTray[2][1] = TrayCreateItem($aTray[2][0]) $aTray[3][1] = TrayCreateItem($aTray[3][0]) $aTray[4][1] = TrayCreateItem($aTray[4][0]) $aTray[5][1] = TrayCreateItem($aTray[5][0]) $aTray[6][1] = TrayCreateItem($aTray[6][0]) $aTray[7][1] = TrayCreateItem($aTray[7][0]) While 1 Switch TrayGetMsg() Case $aTray[0][1] ; "Test 1" Change Test 5 Text. $TrayText = InputBox("Test", "Choose text for Test 5", "Test 5 Delete Me") TrayItemSetText($aTray[4][1], $TrayText) Case $aTray[1][1] ; "Test 2" Deletes "Test 5". TrayItemDelete($aTray[4][1]) $aTray[4][1] = 999 Case $aTray[2][1] ; "Test 3" Restores "Test 5". $aTray[4][1] = TrayCreateItem($aTray[4][0]) Case $aTray[3][1] ; "Test 4" Check if Test 5 value is blank, space, or filled. If $aTray[4][1] = "" Then MsgBox(0, "Test", "Test 5 is blank") ElseIf $aTray[4][1] = " " Then MsgBox(0, "Test", "Test 5 is not blank (space)") Else MsgBox(0, "Test", "Test 5 is assigned a value") EndIf Case $aTray[4][1] ; "Test 5" (Item to test for, during, and after deletion). MsgBox(0, "Test", "I'm here!") Case $aTray[5][1] ; "Test 6" Displays Text from Test 5 item. $Test5Text = TrayItemGetText($aTray[4]) MsgBox(0, "Test", "Test 5 Text: " & $Test5Text) Case $aTray[6][1] ; "Test 7" Displays all item values. MsgBox(0, "Test", "$aTray[0]: " & $aTray[0][1] & @CRLF & _ "$aTray[1]: " & $aTray[1][1] & @CRLF & _ "$aTray[2]: " & $aTray[2][1] & @CRLF & _ "$aTray[3]: " & $aTray[3][1] & @CRLF & _ "$aTray[4]: " & $aTray[4][1] & @CRLF & _ "$aTray[5]: " & $aTray[5][1] & @CRLF & _ "$aTray[6]: " & $aTray[6][1] & @CRLF & _ "$aTray[7]: " & $aTray[7][1] & @CRLF) Case $aTray[7][1] ; "Test 8" Counts all blanks in tray values. Global $blankCount = _ArrayFindAll($aTray, 999) If $blankCount = -1 Then If @error = 6 Then MsgBox(0, "Test", "Error, No blanks present") EndIf Else MsgBox(0, "Test", "# of blanks: " & $blankCount) EndIf EndSwitch WEnd Func Abort() Exit EndFunc ;==>Abort D3fr0s7 1 SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
D3fr0s7 Posted November 19, 2022 Author Posted November 19, 2022 (edited) @Jos Thank you! You helped solve the problem of the controlID's jumbling up and cross referencing each other! Now, the tray items do not run the code of the tray item before it. Now I'm just dealing with the issue of counting "blanks" or in this new case, "999's". Attached is your new code with some clarifying adjustments. expandcollapse popup#include <TrayConstants.au3> #include <Array.au3> HotKeySet("{ESC}", "Abort") Opt("TrayMenuMode", 3) TraySetState($TRAY_ICONSTATE_SHOW) ; Show the tray menu. Global $TrayText Global $aTray[8][2] ; Defines array to hold tray items. $aTray[0][0] = "Test 1 (Name Test 5)" $aTray[1][0] = "Test 2 (Delete Test 5)" $aTray[2][0] = "Test 3 (Restore Test 5)" $aTray[3][0] = "Test 4 (Check if Test 5 controlID is blank or 999)" $aTray[4][0] = "Test 5 Delete Me" $aTray[5][0] = "Test 6 (Check Test 5 Text)" $aTray[6][0] = "Test 7 (Read controlIDs)" $aTray[7][0] = "Test 8 (Count '999's)" $aTray[0][1] = TrayCreateItem($aTray[0][0]) $aTray[1][1] = TrayCreateItem($aTray[1][0]) $aTray[2][1] = TrayCreateItem($aTray[2][0]) $aTray[3][1] = TrayCreateItem($aTray[3][0]) $aTray[4][1] = TrayCreateItem($aTray[4][0]) $aTray[5][1] = TrayCreateItem($aTray[5][0]) $aTray[6][1] = TrayCreateItem($aTray[6][0]) $aTray[7][1] = TrayCreateItem($aTray[7][0]) While 1 Switch TrayGetMsg() Case $aTray[0][1] ; "Test 1" Change Test 5 Text. $TrayText = InputBox("Test", "Choose text for Test 5", "Test 5 Delete Me") TrayItemSetText($aTray[4][1], $TrayText) Case $aTray[1][1] ; "Test 2" Deletes "Test 5". TrayItemDelete($aTray[4][1]) $aTray[4][1] = 999 Case $aTray[2][1] ; "Test 3" Restores "Test 5". $aTray[4][1] = TrayCreateItem($aTray[4][0]) Case $aTray[3][1] ; "Test 4" Check if Test 5 value is blank, space, or filled. If $aTray[4][1] = "" Then MsgBox(0, "Test", "Test 5 controlID is blank" & @CRLF & _ "Text is: " & TrayItemGetText($aTray[4][1]) ) ElseIf $aTray[4][1] = 999 Then MsgBox(0, "Test", "Test 5 is not blank (999)" & @CRLF & _ "Text is: " & TrayItemGetText($aTray[4][1]) ) Else MsgBox(0, "Test", "Test 5 has a controlID" & @CRLF & _ "Text is: " & TrayItemGetText($aTray[4][1]) ) EndIf Case $aTray[4][1] ; "Test 5" (Item to test for, during, and after deletion). MsgBox(0, "Test", "I'm here!") Case $aTray[5][1] ; "Test 6" Displays Text from Test 5 item. $Test5Text = TrayItemGetText($aTray[4][1]) MsgBox(0, "Test", "Test 5 Text: " & $Test5Text) Case $aTray[6][1] ; "Test 7" Displays all controlIDs. MsgBox(0, "Test", "Test 1 controlID: " & $aTray[0][1] & @CRLF & _ "Test 2 controlID: " & $aTray[1][1] & @CRLF & _ "Test 3 controlID: " & $aTray[2][1] & @CRLF & _ "Test 4 controlID: " & $aTray[3][1] & @CRLF & _ "Test 5 controlID: " & $aTray[4][1] & @CRLF & _ "Test 6 controlID: " & $aTray[5][1] & @CRLF & _ "Test 7 controlID: " & $aTray[6][1] & @CRLF & _ "Test 8 controlID: " & $aTray[7][1] ) Case $aTray[7][1] ; "Test 8" Counts all '999' values in array controlIDs. Global $999Count = _ArrayFindAll ( $aTray, 999 ) If $999Count = -1 Then If @error = 6 Then MsgBox(0, "Test", "Error, No '999' present" & @CRLF & _ "Blank count: " & $999Count ) EndIf Else MsgBox(0, "Test", "# of blanks: " & $999Count) EndIf EndSwitch WEnd Func Abort() Exit EndFunc ;==>Abort Now I'm focusing on case $aTray[7][1] "Test 8" that counts all "999" values in the 2D array. I guess now I'm just having trouble understanding how _ArrayFindAll works. It doesn't work to find "999" after deletion on the 2D array as coded. I've also tried, after a bit of reading into the problem in other posts, using _ArrayFindAll ( $aTray, 999, 0, 0, 0, 0, 1) which DOES find a "999" value after deletion, but does not count it when you want it to display the count in the msgbox. I was under the impression that the count would be 1 if it found one iteration of the value it is searching for. Am I mistaken? Thanks again for your help I understand that my original problem was addressed and fixed already and that technically this new problem I'm presented with is a separate issue regarding the use of _ArrayFindAll and if I need to create a new thread to get it fixed, I can Edit: Also, if you can explain how making it a 2D array prevents the problem I was having I'd be grateful. I try to understand the solutions I'm given and I don't fully understand why your solution solved the problem other than that the 2D array was declared instead. Is it because the controlID wasn't erased and instead assigned a value 999 as a placeholder? Edit 2: I figured it out, for anybody stumbling across this and are wondering the same thing (probably not), _ArrayFindAll returns an ARRAY, I was under the impression that it was just a number representing the # of iterations of the search term found in the array being searched. I needed to count the # of rows in the new array to find the # of 999s in the original array. Edited November 21, 2022 by D3fr0s7 Closing off the thread
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now