ContextMenuStrip 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 3 years and 11 months 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
Domtar
Posts: 133
Last visit: Mon Mar 11, 2024 5:38 am
Has voted: 2 times

ContextMenuStrip question

Post by Domtar »

hi everyone,

in a GUI i'm working on, i would like to use the same ContextMenuStrip that i would associate to a few datagridview to select an item. since it's the same function that is running, it's only logical to use the same strip.

I've created 2 datagridview, associated my strip to each, now i'm trying to understand how to get the calling control.

is there a way to identify the control who called the strip?

thank you!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: ContextMenuStrip question

Post by jvierra »

Use the same event for all controls that share the strip. The current control that the strip is associated with is the "$contextmenustrip1.SourceControl"

Example:

Code: Select all

$testToolStripMenuItem_Click={
	Write-Host $contextmenustrip1.SourceControl.Name
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: ContextMenuStrip question

Post by jvierra »

Here is a better example:

Code: Select all

$testToolStripMenuItem_Click={
	Write-Host $this.Name
	Write-Host $contextmenustrip1.SourceControl.Name
}

$testerToolStripMenuItem_Click={
	Write-Host $this.Name
	Write-Host $contextmenustrip1.SourceControl.Name
}
User avatar
Domtar
Posts: 133
Last visit: Mon Mar 11, 2024 5:38 am
Has voted: 2 times

Re: ContextMenuStrip question

Post by Domtar »

exactly what i was looking for :-)

thank you J.
This topic is 3 years and 11 months 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