1

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?

1
  • Option will be enabled for this form only, I am not looking a general mouse-right-click solution from registry
    – igor
    Dec 3, 2017 at 13:29

1 Answer 1

2

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"
$form1.Controls.Add($textBox1)

$form1.ShowDialog()
1
  • Credit to Reza Aghaei from stackoverflow
    – igor
    Dec 4, 2017 at 7:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .