Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, June 7, 2018 3:59 PM
I found the following code but do not know how to add event function, something like OnClick() that will run when clicking on menu items. Please advise. Thanks.
$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()
All replies (3)
Thursday, June 7, 2018 4:21 PM ✅Answered
Not sure if you can have this for text boxes, you might have to use focus on that instead of add click.
$objectname.add_click({ #code in here to run when clicked })
Hope this helps.
Thursday, June 7, 2018 4:52 PM ✅Answered
$menu1 = $contextMenuStrip1.Items.Add("Item 1")
$menu1 = $contextMenuStrip1.Items.Add("Item 1")
$menu1.add_Click({Write-Host $this.Text})
$menu2 = $contextMenuStrip1.Items.Add("Item 2")
$menu2.add_Click({Write-Host $this.Text})
\(ツ)_/
Thursday, June 7, 2018 4:46 PM
Thanks for your answer.
I added in $contextMenuStrip1.Add_click({ Write-Host 'Clicked Item 1' })
and it works when clicking on 'Item 1' or 'Item 2'
How to detect which item clicked in the click event?
$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")
$contextMenuStrip1.Add_click({ Write-Host 'Clicked Item 1' })
$textBox1.ShortcutsEnabled = $false
$textBox1.ContextMenuStrip = $contextMenuStrip1
$form1.Text="Context Menu for TextBox"
$form1.Controls.Add($textBox1)
$form1.ShowDialog()