0

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 to no avail.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Does this answer your question? [How to add sub menu items in contextmenustrip using C#4.0?](https://stackoverflow.com/questions/5868446/how-to-add-sub-menu-items-in-contextmenustrip-using-c4-0) – pringi Nov 08 '19 at 14:14

1 Answers1

0

You should be able to do this to add item3 as a submenu of item2:

#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 })
$toolStripItem2.DropDownItems.Add($toolStripItem3)

Remove Add_Click as needed.

Palle Due
  • 5,929
  • 4
  • 17
  • 32