2

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 right-click menu instead of doing/drawing an actual separate button?

Option will be enabled for this form only, I am not looking a general mouse-right-click solution from registry

1
  • 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. Commented Dec 4, 2017 at 7:15

2 Answers 2

3

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()
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, works as expected. Now I just doing an event function for Item1/2, as clicks, right?
Btw, due line $contextMenuStrip1.Items.Add when running the script, in CMD I can see all item parameter. Do you know how to "silent" it?
assign it to a variable or use | Out-Null to suppress the output.
I love your answer here. I was trying to modify a little script using ContextMenu to use ContexMenuStrip to no avail, but now I can.
0

add [void] the start of $contextMenuStrip1.Items.Add

like so:

[void]$contextMenuStrip1.Items.Add($item)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.