locked
nested submenu in contextstipmenu RRS feed

  • Question

  • hello everyone


    i am experimenting with ContextMenuStrip in windows forms for powershell

     $contextMenuStrip1 = New-Object System.Windows.Forms.ContextMenuStrip
          #creation element1 of menu
          [System.Windows.Forms.ToolStripItem]$toolStripItem1 = New-Object System.Windows.Forms.ToolStripMenuItem
          $toolStripItem1.Text = "AD: Disable User"
          $toolStripItem1.add_Click({ disableuser })
          $contextMenuStrip1.Items.Add($toolStripItem1)
          #creation element2 of menu
          [System.Windows.Forms.ToolStripItem]$toolStripItem2 = New-Object System.Windows.Forms.ToolStripMenuItem
          $toolStripItem2.Text = "AD: Reset password"
          $toolStripItem2.add_Click({ resetpassword })
          $contextMenuStrip1.Items.Add($toolStripItem2)
          #creation element3 of menu
          [System.Windows.Forms.ToolStripItem]$toolStripItem3 = New-Object System.Windows.Forms.ToolStripMenuItem
          $toolStripItem3.Text = "AD: Move to another OU"
          $toolStripItem3.add_Click({ moveADuser })
          $contextMenuStrip1.Items.Add($toolStripItem3)

    i want to create a nested menu though, i have searched the net with no avail, any hints or tips would be highly appreciated.


    Thank you




    Friday, November 8, 2019 12:46 PM

All replies

  • There are thousands of examples and tutorials on using the ToolStrip control. Start by reading the documentation for the control.

    https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/toolstrip-control-overview-windows-forms

    From your question it sounds like you want to use a "MenuStrip" item.

    https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/toolstrip-control-architecture

    When working with any control for the first time us experienced programmers always start by carefully reading the documentation for the control.  For more complex controls this review can take over an hour for correct understanding of the control.  The docs also contain many examples of how to use the features of the control.

    See: https://www.google.com/search?newwindow=1&sxsrf=ACYBGNQVlIAL-QhEUcOdtDW8fUGkmBcN9Q%3A1573233805508&ei=jaTFXbjbHqy-ggeQ4JrYDQ&q=powershell+menustrip&oq=powershell+menustrip&gs_l=psy-ab.3..0j0i22i30l3.280967.288009..289102...0.1..0.477.2732.15j1j1j1j2......0....1..gws-wiz.......0i71j0i67j0i10.XM8YD11t6eI&ved=0ahUKEwj41YDRkNvlAhUsn-AKHRCwBtsQ4dUDCAo&uact=5#spf=1573234095955


    \_(ツ)_/

    Friday, November 8, 2019 5:28 PM
  • For the contextMenuStrip hee are the docs: https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-add-menu-items-to-a-contextmenustrip

    The subitems can be added to the menu item the same way the menuitems are added to the context menu itself.

            # contextmenustrip1
       	$contextmenustrip1 = New-Object 'System.Windows.Forms.ContextMenuStrip'
    	$menuItem1ToolStripMenuItem = New-Object 'System.Windows.Forms.ToolStripMenuItem'
    	$submenuIItem1ToolStripMenuItem = New-Object 'System.Windows.Forms.ToolStripMenuItem'
    
            # add the first menu item
    	[void]$contextmenustrip1.Items.Add($menuItem1ToolStripMenuItem)
    	$contextmenustrip1.Name = 'contextmenustrip1'
    	$contextmenustrip1.Size = '153, 48'
    
            # menuItem1ToolStripMenuItem
            # add the submenu item t themenu item and set properties
    	[void]$menuItem1ToolStripMenuItem.DropDownItems.Add($submenuIItem1ToolStripMenuItem)
    	$menuItem1ToolStripMenuItem.Name = 'menuItem1ToolStripMenuItem'
    	$menuItem1ToolStripMenuItem.Size = '152, 22'
    	$menuItem1ToolStripMenuItem.Text = 'Menu Item 1'
    	$menuItem1ToolStripMenuItem.add_Click($menuItem1ToolStripMenuItem_Click)
    
    	# submenuIItem1ToolStripMenuItem
            # set properties of submenu item
    	$submenuIItem1ToolStripMenuItem.Name = 'submenuIItem1ToolStripMenuItem'
    	$submenuIItem1ToolStripMenuItem.Size = '164, 22'
    	$submenuIItem1ToolStripMenuItem.Text = 'Submenu IItem 1'


    \_(ツ)_/

    Friday, November 8, 2019 6:24 PM
  • In PowerShell the following is not necessary.

    [System.Windows.Forms.ToolStripItem]$toolStripItem3 = New-Object System.Windows.Forms.ToolStripMenuItem
      

    PowerShell handles this internally so we only need to do this:

    $toolStripItem3 = New-Object System.Windows.Forms.ToolStripMenuItem
      
    For modern PowerShell we can also do this with Net types:

    $contextMenuStrip1 =[System.Windows.Forms.ContextMenuStrip]::New()



    \_(ツ)_/

    Friday, November 8, 2019 6:27 PM
  • Here is a working example:

    function Show-Form1{
    
        Add-Type -AssemblyName System.Windows.Forms
        
    	$form1 = [System.Windows.Forms.Form]::new()
    
        # create menu control and add items
       	$contextmenustrip1 = [System.Windows.Forms.ContextMenuStrip]::new()
    	$menuItem1ToolStripMenuItem = [System.Windows.Forms.ToolStripMenuItem]::new()
    	[void]$contextmenustrip1.Items.Add($menuItem1ToolStripMenuItem)
    	$submenuIItem1ToolStripMenuItem = [System.Windows.Forms.ToolStripMenuItem]::new()
    	[void]$menuItem1ToolStripMenuItem.DropDownItems.Add($submenuIItem1ToolStripMenuItem)
    
        # configure form
        $form1.StartPosition = 'CenterScreen'
    	$form1.ContextMenuStrip = $contextmenustrip1
    
        # configure the menu items
        
        #contextmenustrip1
    	$contextmenustrip1.Name = 'contextmenustrip1'
    	$contextmenustrip1.Size = '153, 48'
    
        # menuItem1ToolStripMenuItem
    	$menuItem1ToolStripMenuItem.Name = 'menuItem1ToolStripMenuItem'
    	$menuItem1ToolStripMenuItem.Size = '152, 22'
    	$menuItem1ToolStripMenuItem.Text = 'Menu Item 1'
    	$menuItem1ToolStripMenuItem.add_Click($menuItem1ToolStripMenuItem_Click)
    
    	# submenuIItem1ToolStripMenuItem
    	$submenuIItem1ToolStripMenuItem.Name = 'submenuIItem1ToolStripMenuItem'
    	$submenuIItem1ToolStripMenuItem.Size = '164, 22'
    	$submenuIItem1ToolStripMenuItem.Text = 'Submenu IItem 1'
        $form1.ShowDialog()
    }
    
    Show-Form1


    \_(ツ)_/

    Friday, November 8, 2019 6:42 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, December 6, 2019 9:41 AM