Support Forums

contextmenustrip opening event question

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.
User avatar
tbednarz
Posts: 17
Joined: Tue Jan 12, 2016 3:09 am

contextmenustrip opening event question

Postby tbednarz » Wed Feb 10, 2016 1:12 am

I added a contextmenustrip control zu a form created with PowerShell Studio 2015. This form has TabControls and within the tabs many other controls. The context menu appears everywhere when I right-click with the mouse!

So I implemented the event contextmenustrip_Opening and set $_.Cancel = $true. The result is that it does not appear anywhere. I try to figure out, where the user clicked but that seems to be hard.... I did:

$control = $contextmenustrip1.SourceControl

$control contains always System.Windows.Forms.TabControl, TabPages.Count: 3, ..... etc, whatever the toString method returns. I rather would expect something like treecontrol1 or GridListView1 etc. How do I get this information? In the Opening Event I like to build a context menu depending on the control where the user clicked.
User avatar
jvierra
Posts: 10030
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: contextmenustrip opening event question

Postby jvierra » Wed Feb 10, 2016 2:10 am

Attach context menu to the control you need it to apply to. You have attached it to the main form so it will open everywhere.
User avatar
tbednarz
Posts: 17
Joined: Tue Jan 12, 2016 3:09 am

Re: contextmenustrip opening event question

Postby tbednarz » Wed Feb 10, 2016 2:23 am

Hmm I am probably too stupid, but when I drag it in the designer, it is sort of everywhere. In the following article (https://www.sapien.com/blog/2011/12/16/ ... p-control/) is written:
For your convenience, when you drop a ContextMenuStrip in the designer, PrimalForms will automatically assign the ContextMenuStrip to any control you drop it on.


So I have set the property 'ContextMenuStrip' of the datagridview where I like to show it up to 'contextmenustrip1'. But it still shows up everywhere.... Does it mean I have to create it manually e.g. on FormLoad WITHOUT the designer and then assign it to the mentioned propertiy?
User avatar
jvierra
Posts: 10030
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: contextmenustrip opening event question

Postby jvierra » Wed Feb 10, 2016 2:42 am

Go to properties of the control you want to use this on and select "ContextMenu" property and set it to the menu you want.

Return to “PowerShell GUIs”

Who is online

Users browsing this forum: No registered users and 7 guests

cron