0

Have the following from How to add options to mouse-right-click menu in PowerShell forms

$contextMenuStrip1.Items.Add("Item 1") | Out-Null
$contextMenuStrip1.Items.Add("Item 2") | Out-Null

And it is working... I can see the context menu.. but how do I do something once the menu item has been selected.

Like if a user selects Item 1 how do I know it was selected.

  • Add an event handler. – DavidPostill Aug 11 '18 at 20:24
  • That's helpful for a beginner/noob like me. – nixgeek Aug 15 '18 at 20:10
0

As DavidPostill guidance leads... There loads of videos, articles and sample code that takes on thru this sort of thing, step-by-step. For your query, here is an example of what DavidPostill is saying.

$textBox1.Add_Click(
    {    
        [System.Windows.Forms.MessageBox]::Show("Hello World." , "My Dialog Box")
    }
)

$form1.ShowDialog()

See https://social.technet.microsoft.com/wiki/contents/articles/25911.how-to-add-a-powershell-gui-event-handler-part-1.aspx

  • Ok, I can follow along with that for a textbox. But the same applies to the contextmenustrip1 object? So how I determine which menu item was selected? is it numbered somehow? would I add something like $contextMenuStrip1.Add_Click? – nixgeek Aug 15 '18 at 20:13
  • @nixgeek You need to learn to walk before you can run. Learn the basics before trying to modify random bits of code you found on the internet. – DavidPostill Aug 15 '18 at 20:15
  • Ok, I figured out how to do this... Instead of just adding Context Menus. I also added as David Pointed out the Event Handler. I replaced: $contextMenuStrip1.Items.Add("Item 1") | Out-Null $contextMenuStrip1.Items.Add("Item 2") | Out-Null With this: $contextMenuStrip1.Items.Add("Item1", $ContextMenuStripItemImages).add_Click({ MyCode Here }) $contextMenuStrip1.Items.Add("Item2", $ContextMenuStripItemImages).add_Click({ MyCode Here }) – nixgeek Aug 15 '18 at 23:41
0

Ok, I figured out how to do this... Instead of just adding Context Menus. I also added as David pointed out the Event Handler.

I replaced:

  $contextMenuStrip1.Items.Add("Item 1") | Out-Null
  $contextMenuStrip1.Items.Add("Item 2") | Out-Null

With this:

  $contextMenuStrip1.Items.Add("Item1", $ContextMenuStripItemImages).add_Click({ MyCode Here })
  $contextMenuStrip1.Items.Add("Item2", $ContextMenuStripItemImages).add_Click({ MyCode Here })

Works great now.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.