How to add options to mouse-right-click menu in PowerShell forms

mousepowershellright clickwinforms

I have a basic PowerShell form with a textbox. When I right-click on the textbox, a standard menu appears with Copy, Cut, Paste, etc…

My goal is to add a "clear log" option, that clears current textbox content. How can I add this option to the right-click menu instead of doing/drawing an actual separate button?

Best Answer

To be able to show a ContextMenuStrip for a TextBox first you should set ShortcutsEnabled property of the TextBox to false, then assign a ContextMenuStrip to its ContextMenuStrip property like this:

$form1= New-Object System.Windows.Forms.Form
$textBox1 = New-Object System.Windows.Forms.TextBox
$contextMenuStrip1 = New-Object System.Windows.Forms.ContextMenuStrip

$contextMenuStrip1.Items.Add("Item 1")
$contextMenuStrip1.Items.Add("Item 2")

$textBox1.ShortcutsEnabled = $false
$textBox1.ContextMenuStrip = $contextMenuStrip1

$form1.Text="Context Menu for TextBox"

Related Question