peterlustig
 New Member Posts:6

 |
| 11 Mar 2010 01:50 PM |
|
Hello there,
i have a little problem to generate a Context Menu in Powershell. I have built a form with different window forms on it (e.g. a normal textbox) via Primal Forms CE Now i want to generate a context menu with different features in it and link it to the textbox: If the textbox is right clicked a menu opens with different entries in it. Now i have some problems in realising it:
1. How can i build a new context menu 2. How can i add new items to the menu 3. How can i link it with a special form (e.g. textbox) 4. How can i use handler for the different items in the context menu
Would be nice if somebody can help me here.
Thanks in advance
Regards, Tom
|
|
|
|
|
GWHowarth88
 Basic Member Posts:336

 |
| 11 Mar 2010 02:22 PM |
|
All types deriving from System.Windows.Forms.Control have a ContextMenu property. Setting it up should be pretty straight forward:
$contextMenu = New-Object System.Windows.Forms.ContextMenu # Create context menu $menuItem1 = New-Object System.Windows.Forms.MenuItem -ArgumentList "Hello" $menuItem2 = New-Object System.Windows.Forms.MenuItem -ArgumentList "Hello Again"
$contextMenu.MenuItems.Add($menuItem1) # Add option to context menu $contextMenu.MenuItems.Add($menuItem2)
$textBox.ContextMenu = $contextMenu # Assuming you've assigned your TextBox instance to $textBox
As for the event handler, I'm not entirely sure about that, but looking at this blog: http://blogs.msdn.com/powershell/archive/2008/05/24/wpf-powershell-part-3-handling-events.aspx
... it looks like it's done this way:
$menuItem1.Add_Click({ [System.Windows.Forms.MessageBox]::Show("Hello Clicked!") }) $menuItem2.Add_Click({ [System.Windows.Forms.MessageBox]::Show("Hello Again Clicked!") }) |
|
|
|
|
GWHowarth88
 Basic Member Posts:336

 |
| 11 Mar 2010 02:49 PM |
|
Sorry I re-read your question and realised you were using PrimalForms, not WinForms. I think its a slightly different syntax for event handling, plus a ContextMenuStrip may be better if you're not bothered about backwards compatability: $contextMenuStrip = New-Object System.Windows.Forms.ContextMenuStrip # Create context menu $menuItem1 = New-Object System.Windows.Forms.ToolStripMenuItem -ArgumentList "Hello" $menuItem1_OnClick({ [System.Windows.Forms.MessageBox]::Show("Hello Clicked!") }) $menuItem2 = New-Object System.Windows.Forms.ToolStripMenuItem -ArgumentList "Hello Again" $menuItem2_OnClick({ [System.Windows.Forms.MessageBox]::Show("Hello Again Clicked!") }) $contextMenuStrip.Items.Add($menuItem1) # Add option to context menu $contextMenuStrip.Items.Add($menuItem2) $textBox.ContextMenuStrip = $contextMenuStrip # Assuming you've assigned your TextBox instance to $textBox |
|
|
|
|
peterlustig
 New Member Posts:6

 |
| 11 Mar 2010 03:25 PM |
|
Hello GWHowarth, thanks so far, the creation of context menu is working, also the "add option". The event handling is not working. I changed the "on_click" event to the following because i got some errors in ps: $menuItem2_OnClick= { [System.Windows.Forms.MessageBox]::Show("Hello Again Clicked!") } Now the errors are gone and the programm is starting, but nothing happens when i choose one of the items. Do you perhaps have a idea again? Thank you for your help Regards, Tom |
|
|
|
|
GWHowarth88
 Basic Member Posts:336

 |
| 11 Mar 2010 03:31 PM |
|
Try this instead: $contextMenuStrip = New-Object System.Windows.Forms.ContextMenuStrip # Create context menu $menuItem1 = New-Object System.Windows.Forms.ToolStripMenuItem -ArgumentList "Hello" $menuItem1.Add_Click($menuItem1_OnClick) $menuItem2 = New-Object System.Windows.Forms.ToolStripMenuItem -ArgumentList "Hello Again" $menuItem2.Add_Click($menuItem2_OnClick) $contextMenuStrip.Items.Add($menuItem1) # Add option to context menu $contextMenuStrip.Items.Add($menuItem2) $menuItem1_OnClick=({ [System.Windows.Forms.MessageBox]::Show("Hello Clicked!") }) $menuItem2_OnClick=({ [System.Windows.Forms.MessageBox]::Show("Hello Again Clicked!") }) $textBox.ContextMenuStrip = $contextMenuStrip # Assuming you've assigned your TextBox instance to $textBox |
|
|
|
|
peterlustig
 New Member Posts:6

 |
| 11 Mar 2010 03:35 PM |
|
Hello, just got it: $menuitem1.add_click({[System.Windows.Forms.MessageBox]::Show("Hello Clicked!") }) You already wrote it in your first post. Thanks a lot for your help.
|
|
|
|
|