powershell winforms context menu

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.

nixgeek

Posted 2018-08-11T19:28:49.497

Reputation: 1

Add an event handler. – DavidPostill – 2018-08-11T20:24:33.607

That's helpful for a beginner/noob like me. – nixgeek – 2018-08-15T20:10:54.763

Answers

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

postanote

Posted 2018-08-11T19:28:49.497

Reputation: 1 783

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 – 2018-08-15T20:13:02.357

@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 – 2018-08-15T20:15:14.557

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 – 2018-08-15T23:41:08.990

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.

nixgeek

Posted 2018-08-11T19:28:49.497

Reputation: 1