locked
ISE freeze when i call ToolStripMenuItem RRS feed

  • Question

  • Hi everyone,

    i´m trying to do a notifyIcon with a menu, but when i right-click my notifyIcon, show a context menu frozen.

    Code:

    cls
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")##
    
    $objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon
    $contextMenu = New-Object System.Windows.Forms.ContextMenuStrip
    $MenuItem1 = New-Object System.Windows.Forms.ToolStripMenuItem
    $MenuItem2 = New-Object System.Windows.Forms.ToolStripMenuItem
    
    $MenuItem1.Text = "Menu Item 1"
    $MenuItem2.Text = "Menu Item 2"
    
    Get-EventSubscriber -Force | Unregister-Event -Force
    
    register-objectevent -InputObject $objNotifyIcon -EventName BalloonTipClicked -SourceIdentifier objNotifyIcon.BalloonTipClicked -Action     {$objNotifyIcon.Dispose()} | Out-Null
    register-objectevent -InputObject $objNotifyIcon -EventName DoubleCLick -SourceIdentifier objNotifyIcon.DoubleClick -Action     {$objNotifyIcon.ShowBalloonTip(3000)} | Out-Null
    
    register-objectevent -InputObject $MenuItem1 -EventName Click -SourceIdentifier MenuItem1.DoubleClick -Action     {[System.Windows.Forms.MessageBox]::Show(“MenuItem1 Clicked”,”Information”)} | Out-Null
    register-objectevent -InputObject $MenuItem2 -EventName Click -SourceIdentifier MenuItem2.DoubleClick -Action     {[System.Windows.Forms.MessageBox]::Show(“MenuItem2 Clicked”,”Information”)} | Out-Null
    
    $PC = "MMRdb01"
    
    $objNotifyIcon.Icon = "C:\Users\dvsilva\Documents\Projeto Balanças\icons\icon_Alert.ico"
    $objNotifyIcon.BalloonTipIcon = "Error" 
    $objNotifyIcon.BalloonTipText = "Servidor $pc não está respondendo. Clique para fechar a notificação." 
    $objNotifyIcon.BalloonTipTitle = "$pc não alcansado"
    $objNotifyIcon.Visible = $true
    $objNotifyIcon.ContextMenuStrip = $contextMenu
    $objNotifyIcon.ContextMenuStrip.Items.AddRange(@($MenuItem1, $MenuItem2))
    
    $MenuItem1.Enabled = $false
    $MenuItem2.Enabled = $false
    $contextMenu.Enabled = $false
    
    
    $objNotifyIcon.ShowBalloonTip(3000)

    need to know how i use a contextmenustrip in my notify icon.

    Thanks.


    Friday, March 4, 2016 12:17 PM

Answers

  • NotifyIcon control only works correctly when called  from a form.  It is not compatible with a console(windowless) program.

    \_(ツ)_/

    • Marked as answer by DaniloVSilva Monday, March 7, 2016 5:12 PM
    Monday, March 7, 2016 1:01 PM

All replies

  • didn´t work.

    isn´t the same problem, and i tried to use that too...

    $ContextMenu.Items.Add($MenuItem1)
    $objNotifyIcon.ContextMenuStrip.Items.Add($MenuItem1)

    More one thing Jrv, i'm now utilizing the strip menu tools cause the other of the past (contextmenu, menuitem) didnt work with just one click.... i mean... when i'm using the contextmenu + menuitem i need to click twice to execute the "click" event of the menuitem. hard life.

    someone? ideas please?

    Friday, March 4, 2016 8:01 PM
  • Here - its fixed:

    Add-Type -AssemblyName System.Windows.Forms
    $ni = New-Object System.Windows.Forms.NotifyIcon
    $ni.Visible=$true
    $ni.Icon = 'C:\test\bing.ico' #[System.Drawing.SystemIcons]::Information
    $cm = New-Object System.Windows.Forms.ContextMenu
    $ni.ContextMenu = $cm
    $cm.Name='cm1'
    $mi = New-Object System.Windows.Forms.MenuItem
    $mi.Name='meuitem1'
    $mi.Text='My menu item'
    $cm.MenuItems.Add($mi)
    
    $clicked={
    	#https://msdn.microsoft.com/en-us/library/system.management.automation.pseventargs_members%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396
    	Write-Host 'Menu Item Clicked' -fore green
    	Write-Host $Event.TimeGenerated -fore Blue -back white
    	Write-Host $Event.Sender.Text -fore black -back white
    }
    $evt=Register-ObjectEvent $ni Click -sourceIdentifier clicked -Action $clicked
    
    $ni.BalloonTipTitle='This is a Balloon Title'
    $ni.BalloonTipText='This is the message in the balloon tip'
    $ni.BalloonTipIcon='Error'
    $ni.Visible=$True 
    #$ni.ShowBalloonTip(2000)
    
    


    \_(ツ)_/

    Friday, March 4, 2016 8:42 PM
  • (...)More one thing Jrv, i'm now utilizing the strip menu tools cause the other of the past (contextmenu, menuitem) didnt work with just one click.... i mean... when i'm using the contextmenu + menuitem i need to click twice to execute the "click" event of the menuitem.

    (...)

    =/

    but thx try to help.

    Monday, March 7, 2016 12:57 PM
  • NotifyIcon control only works correctly when called  from a form.  It is not compatible with a console(windowless) program.

    \_(ツ)_/

    • Marked as answer by DaniloVSilva Monday, March 7, 2016 5:12 PM
    Monday, March 7, 2016 1:01 PM
  • thx Jrv, more one time.

    :)

    Monday, March 7, 2016 5:14 PM