none
MenuStrip Class RRS feed

  • Question

  • I'm implementing successfully the mouse over event to all the ToolsStripMenuItems I added to the MenuStrip. The action I'm implementing is the Cursor method to change to the Hand property but I get an error. How can I get bypass this error.


    michael john ocasio

    Thursday, August 18, 2016 2:18 PM

Answers

All replies

  • And that error is?

    Thursday, August 18, 2016 2:19 PM
  •  function SetCursorToHand ([System.Windows.Forms.MenuStrip]$menu)
       {
         $menu.Cursor = [System.Windows.Forms.Cursors]::Hand
       }

    Property 'Cursor' cannot be found on this object; make sure it exists and is settable.
    At C:\temp\Scripts\File.ps1:358 char:6
    +      $menu.Cursor = [System.Windows.Forms.Cursors]::Hand
    +      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : PropertyNotFound


    michael john ocasio


    • Edited by mjocasio23 Thursday, August 18, 2016 2:39 PM
    Thursday, August 18, 2016 2:33 PM
  • $menu | Get-Member

    Thursday, August 18, 2016 2:33 PM
  • https://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripmenuitem(v=vs.110).aspx

    A MenuStrip and ToolStrip have "Cursor" properties but the menu items do not.


    \_(ツ)_/


    • Edited by jrv Thursday, August 18, 2016 2:44 PM
    Thursday, August 18, 2016 2:42 PM
  •  function menuTest
       {
       function SetCursorToHand ([System.Windows.Forms.MenuStrip]$menu)
       {
         $menu.Cursor = [System.Windows.Forms.Cursors]::Hand
       }

        $Form = New-Object System.Windows.Forms.Form
        $MenuBar = New-Object System.Windows.Forms.MenuStrip
        $FileMenu = New-Object System.Windows.Forms.ToolStripMenuItem
        $FileOpen = New-Object System.Windows.Forms.ToolStripMenuItem
        $FileClose = New-Object System.Windows.Forms.ToolStripMenuItem
        $UserGMenu = New-Object System.Windows.Forms.ToolStripMenuItem  
        $UserOUMenu = New-Object System.Windows.Forms.ToolStripMenuItem
        $UserDivMenu = New-Object System.Windows.Forms.ToolStripMenuItem
        $ExchangeGroupUserMenu = New-Object System.Windows.Forms.ToolStripMenuItem
        $Help = New-Object System.Windows.Forms.ToolStripMenuItem

        $MenuBar.Add_MouseHover({SetCursorToHand})
        $Form.Controls.Add($MenuBar)

         $FileMenu.Text = "&File"
       
        $FileOpen.Text = "&Open"
        $FileOpen.Name = "Open"
        $FileOpen.Image = [System.Drawing.Image]::FromFile("$PWD\Icon\Preview.png")
        $FileMenu.DropDownItems.Add($FileOpen)
        $FileOpen.Add_Click( { OnClick_Open} )
        $FileMenu.Image = [System.Drawing.Image]::FromFile("$PWD\Icon\New_file.png")

        $FileClose.Text = "&Exit"
        $FileClose.Name = "Exit"
        $FileClose.Image = [System.Drawing.Image]::FromFile("$PWD\Icon\Exit.png")
        $FileMenu.DropDownItems.Add($FileClose)
        $FileClose.Add_Click( { OnClick_Close} )
           
        $MenuBar.Items.Add($FileMenu)

        $UserGMenu.Text = "&Group Users"

        $UserGMenu.Image = [System.Drawing.Image]::FromFile("$PWD\Icon\GroupUsers.png.")

        $MenuBar.Items.Add($UserGMenu)
      
        $UserOUMenu.Text = "&Organization Unit"

        $UserOUMenu.Image = [System.Drawing.Image]::FromFile("$PWD\Icon\Work_area.png")

        $MenuBar.Items.Add($UserOUMenu)
      
        $UserDivMenu.Text = "&Division"

        $UserDivMenu.Image = [System.Drawing.Image]::FromFile("$PWD\Icon\Work_area.png")

        $MenuBar.Items.Add($UserDivMenu)

        $ExchangeGroupUserMenu.Image = [System.Drawing.Image]::FromFile("$PWD\Icon\Work_area.png")

        $ExchangeGroupUserMenu.Text = "&Exchange Users"

        $ExchangeGroupUserMenu.Add_Click({OnClick_ExchangeGroupUserMenu})

        $MenuBar.Items.Add($ExchangeGroupUserMenu)
      
        $Help.Text = "&Help"
        $Help.Name = "Help"

        $Help.Image = [System.Drawing.Image]::FromFile("$PWD\Icon\Help.png")
        $Help.Add_Click( { OnClick_Help} )

        $MenuBar.Items.Add($Help)

        $Form.Add_Shown({$Form.Activate()})
       
        #Save the initial state of the form
        $InitialFormWindowState = $form1.WindowState
       
        $Form.ShowDialog()
        }


    michael john ocasio

    Thursday, August 18, 2016 2:49 PM
  • You don't need or want a function.

    function menuTest {
    	
    	$Form = New-Object System.Windows.Forms.Form
    	$MenuBar = New-Object System.Windows.Forms.MenuStrip
    	$FileMenu = New-Object System.Windows.Forms.ToolStripMenuItem
    	$FileOpen = New-Object System.Windows.Forms.ToolStripMenuItem
    	$FileClose = New-Object System.Windows.Forms.ToolStripMenuItem
    	$UserGMenu = New-Object System.Windows.Forms.ToolStripMenuItem
    	$UserOUMenu = New-Object System.Windows.Forms.ToolStripMenuItem
    	$UserDivMenu = New-Object System.Windows.Forms.ToolStripMenuItem
    	$ExchangeGroupUserMenu = New-Object System.Windows.Forms.ToolStripMenuItem
    	$Help = New-Object System.Windows.Forms.ToolStripMenuItem
    	
    	$MenuBar.Add_MouseHover({ $menu.Cursor ='Hand' })
    	$Form.Controls.Add($MenuBar)
    	
    	$FileMenu.Text = "&File"
    	
    	$FileOpen.Text = "&Open"
    	$FileOpen.Name = "Open"
    	$FileOpen.Image = [System.Drawing.Image]::FromFile("$PWD\Icon\Preview.png")
    	$FileMenu.DropDownItems.Add($FileOpen)
    	$FileOpen.Add_Click({ OnClick_Open })
    	$FileMenu.Image = [System.Drawing.Image]::FromFile("$PWD\Icon\New_file.png")
    	
    	$FileClose.Text = "&Exit"
    	$FileClose.Name = "Exit"
    	$FileClose.Image = [System.Drawing.Image]::FromFile("$PWD\Icon\Exit.png")
    	$FileMenu.DropDownItems.Add($FileClose)
    	$FileClose.Add_Click({ OnClick_Close })
    	
    	$MenuBar.Items.Add($FileMenu)
    	
    	$UserGMenu.Text = "&Group Users"
    	
    	$UserGMenu.Image = [System.Drawing.Image]::FromFile("$PWD\Icon\GroupUsers.png.")
    	
    	$MenuBar.Items.Add($UserGMenu)
    	
    	$UserOUMenu.Text = "&Organization Unit"
    	
    	$UserOUMenu.Image = [System.Drawing.Image]::FromFile("$PWD\Icon\Work_area.png")
    	
    	$MenuBar.Items.Add($UserOUMenu)
    	
    	$UserDivMenu.Text = "&Division"
    	
    	$UserDivMenu.Image = [System.Drawing.Image]::FromFile("$PWD\Icon\Work_area.png")
    	
    	$MenuBar.Items.Add($UserDivMenu)
    	
    	$ExchangeGroupUserMenu.Image = [System.Drawing.Image]::FromFile("$PWD\Icon\Work_area.png")
    	
    	$ExchangeGroupUserMenu.Text = "&Exchange Users"
    	
    	$ExchangeGroupUserMenu.Add_Click({ OnClick_ExchangeGroupUserMenu })
    	
    	$MenuBar.Items.Add($ExchangeGroupUserMenu)
    	
    	$Help.Text = "&Help"
    	$Help.Name = "Help"
    	
    	$Help.Image = [System.Drawing.Image]::FromFile("$PWD\Icon\Help.png")
    	$Help.Add_Click({ OnClick_Help })
    	
    	$MenuBar.Items.Add($Help)
    	
    	$Form.Add_Shown({ $Form.Activate() })
    	
    	#Save the initial state of the form 
    	$InitialFormWindowState = $form1.WindowState
    	
    	$Form.ShowDialog()
    }
    

    Don't create functions for actions.

    Example:

    $help_click = {
       # code to display help
    }
    $Help.Add_Click($help_click)
    


    \_(ツ)_/

    Thursday, August 18, 2016 3:00 PM
  • I see thanks.... calling the function is not what is generating my error at run time.... right?

    michael john ocasio

    Thursday, August 18, 2016 3:08 PM
  • Calling the function was your error.

    \_(ツ)_/

    • Marked as answer by mjocasio23 Thursday, August 18, 2016 4:58 PM
    Thursday, August 18, 2016 3:26 PM
  • The call to the function is ok according to the debugger. I am able to reach the segment code on both flavors code...


    michael john ocasio

    • Marked as answer by mjocasio23 Thursday, August 18, 2016 4:58 PM
    • Unmarked as answer by mjocasio23 Thursday, August 18, 2016 4:58 PM
    Thursday, August 18, 2016 3:59 PM
  • you were right....

     $SetCursorToHand =
       {
         $MenuBar.Cursor = [System.Windows.Forms.Cursors]::Hand
       }


    michael john ocasio

    Thursday, August 18, 2016 4:58 PM