Issue with Event Handlers in Tray App for Dynamic Script Execution

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.

This forum is a space to discuss coding in PowerShell, and technical issues related to development.

- Question about a licensed SAPIEN product? Post here: Product Support for Registered Users
- Question about a trial SAPIEN product? Post here: Former and Future Customers - Questions
This topic is 1 month and 1 week old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
Wayssm__
Posts: 1
Last visit: Wed Oct 23, 2024 4:05 am

Issue with Event Handlers in Tray App for Dynamic Script Execution

Post by Wayssm__ »

Hi all,

I'm developing a tray app to dynamically load and run my scripts. Loading directories and their scripts works fine. I also want to include themes, ensuring only one theme can be selected at a time.

However, I'm struggling with getting the items to execute properly using SetMenuHandler or add_Click({}). The items don't seem to respond as expected.

Any ideas on what might be going wrong? I'm also open to suggestions on techniques or concepts I should look into to solve this issue.

Thanks in advance!
  1. FUNCTION Build-ContextMenu {
  2.     IF ($SAPIENHost -ne $null) {
  3.         $Menu = $SAPIENHost.GetContextMenuStrip()
  4.         WHILE ($Menu.Items.Count -gt 2) {
  5.             $Menu.Items.RemoveAt(0)
  6.         }
  7.         Add-ThemeItems
  8.         Add-ScriptItems
  9.     }
  10. }
  11.  
  12. FUNCTION Add-ThemeItems {
  13.     $JsonPath = Join-Path -Path (Get-ScriptDirectory) -ChildPath "Themes.json"
  14.     IF (-not (Test-Path -Path $JsonPath -PathType Leaf)) {
  15.         $null = [System.Windows.Forms.MessageBox]::Show("$JsonPath not found", "404 Not Found", 'OK', 'Error')
  16.         RETURN
  17.     }
  18.    
  19.     $Menu = $SAPIENHost.GetContextMenuStrip()
  20.     $ThemeMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
  21.     $ThemeMenuItem.Name = "Themeauswahl"
  22.     $ThemeMenuItem.Text = "Themeauswahl"
  23.     $Menu.Items.Insert($Menu.Items.Count - 2, $ThemeMenuItem) | Out-Null
  24.    
  25.     $Themes = [System.Collections.Generic.List[System.String]]::new()
  26.     $Json = Get-Content $JsonPath | ConvertFrom-Json
  27.     FOREACH ($key IN $Json.PSObject.Properties) {
  28.         $Themes.Add($key.Name.Trim())
  29.     }
  30.    
  31.     FOREACH ($Theme IN $Themes) {
  32.         $ThemeSubmenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
  33.         $ThemeSubmenuItem.Name = $Theme
  34.         $ThemeSubmenuItem.Text = $Theme
  35.         $ThemeMenuItem.DropDownItems.Add($ThemeSubmenuItem) | Out-Null
  36.        
  37.         $action = {
  38.             Write-Log -Text "Theme has changed - $($this.Text)" -PrintOut
  39.             $global:Theme = $this.Text
  40.             $ThemeMenuItem.DropDownItems | where { $_.Text -ne $this.Text } | foreach { $_.Checked = $false }
  41.             $this.Checked = $true
  42.         }.GetNewClosure()
  43.         Write-Log -Text $ThemeMenuItem.DropDownItems[0].GetType() -PrintOut
  44.        
  45.         $SAPIENHost.SetMenuHandler($ThemeSubmenuItem, $action)
  46.     }
  47.    
  48.     $handler = [System.Windows.Forms.ToolStripItemClickedEventHandler]{
  49.         PARAM ($sender,
  50.             $e)
  51.         Write-Log -Text "Theme clicked... $($e.ClickedItem.Text)" -PrintOut
  52.         $global:Theme = $e.ClickedItem.Text
  53.         $ThemeMenuItem.DropDownItems | where { $_.Text -ne $e.ClickedItem.Text } | foreach { $_.Checked = $false }
  54.         $e.ClickedItem.Checked = $true
  55.     }
  56.     $ThemeMenuItem.add_DropDownItemClicked($handler)
  57. }
  58.  
  59. FUNCTION Add-ScriptItems {
  60.     $Menu = $SAPIENHost.GetContextMenuStrip()
  61.     $scriptDirectories = Locate-Scripts
  62.     FOREACH ($directory IN $scriptDirectories.Keys) {
  63.         $ScriptMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
  64.         $ScriptMenuItem.Name = $directory
  65.         $ScriptMenuItem.Text = $directory.Substring(3)
  66.         FOREACH ($script IN $scriptDirectories[$directory]) {
  67.             $ScriptSubmenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
  68.             $ScriptSubmenuItem.Name = $script
  69.             $ScriptSubmenuItem.Text = $script.Substring(3, $script.Length - 3).Replace("-", " ")
  70.             $SAPIENHost.SetMenuHandler($ScriptSubmenuItem, { Execute-Skript -Directory $directory -Script $script }.GetNewClosure())
  71.             $ScriptMenuItem.DropDownItems.Add($ScriptSubmenuItem) | Out-Null
  72.         }
  73.         $Menu.Items.Insert($Menu.Items.Count - 2, $ScriptMenuItem) | Out-Null
  74.     }
  75. }
This topic is 1 month and 1 week old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked