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.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 8 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
tbednarz
Posts: 17
Last visit: Fri Apr 07, 2017 5:55 am

contextmenustrip opening event question

Post by tbednarz »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: contextmenustrip opening event question

Post by jvierra »

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
Last visit: Fri Apr 07, 2017 5:55 am

Re: contextmenustrip opening event question

Post by tbednarz »

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?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: contextmenustrip opening event question

Post by jvierra »

Go to properties of the control you want to use this on and select "ContextMenu" property and set it to the menu you want.
This topic is 8 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked