header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Generate a Context Menu in Powershell
Last Post 11 Mar 2010 07:35 AM by peterlustig. 5 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Not Resolved
peterlustigUser is Offline
New Member
New Member
Posts:9
Avatar

--
11 Mar 2010 05:50 AM
    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
    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    11 Mar 2010 06:22 AM
    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/ar...vents.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!") })
    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    11 Mar 2010 06:49 AM
    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
    peterlustigUser is Offline
    New Member
    New Member
    Posts:9
    Avatar

    --
    11 Mar 2010 07:25 AM
    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
    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    11 Mar 2010 07:31 AM
    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
    peterlustigUser is Offline
    New Member
    New Member
    Posts:9
    Avatar

    --
    11 Mar 2010 07:35 AM
    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.



    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer