ContextMenuStrip label sensitive

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 7 years and 3 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
DarkLite1
Posts: 102
Last visit: Wed Jan 06, 2021 4:12 am

ContextMenuStrip label sensitive

Post by DarkLite1 »

Product, version and build: PowerShell Studio 5.3.131
32 or 64 bit version of product: 64 bit
Operating system: Win Srv 2008 R2
32 or 64 bit OS: 64 bit
PowerShell Version: 4.0

I've added the same ContextMenuStrip that only contains one menu item called 'Copy' to 2 different labels. When I right click the labels the menu pops-up correctly showing the option 'Copy'. What I'm now struggling with is have it copy the text from the selected label into memory. So it should be aware which label was clicked an depending on that copy the correct label content.

The code below demonstrates what I want it to do. However, it doesn't work with the method 'Focused'.
  1. $ToolStripMenuItemCopy_Click = {
  2.     if ($labelEmail.Focused) {
  3.         Write-Host "Selected email"
  4.     }
  5.     elseif ($labelSerialNumber.Focused) {
  6.         Write-Host "Selected SerialNumber"
  7.     }
  8. }
Of course I can add to every label a different ContextMenuStrip from the designer but I have a lot of labels and would only like to use one ContextMenuStrip as it always needs to present the same optioin 'Copy'. On a side note, when a label is in a 'TableLayoutPanel' it is not possible to drop a ContextMenuStrip on that label in the TableLayoutPanel. This might be a bug or my mistake. An extra thing I noticed is that when dragging a ContextMenuStrip on the form the mouse pointer doesn't come back to an arrow, it stays on a cross which is a bit annoying.

Thank you for your help.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: ContextMenuStrip label sensitive

Post by davidc »

[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]

You can use the contextMenuStrip's SourceControl property to determine what control called it.

As for the TableLayout, that is the expected behavior, since you can only drag on control in each cell.
David
SAPIEN Technologies, Inc.
User avatar
DarkLite1
Posts: 102
Last visit: Wed Jan 06, 2021 4:12 am

Re: ContextMenuStrip label sensitive

Post by DarkLite1 »

Thank you David, you fixed my problem :)
  1. switch ($ToolStripMenuItemCopy.SourceControl) {
  2.         $labelEmail{
  3.             Write-Host "Selected email"
  4.         }
  5.         $labelSerialNumber{
  6.             Write-Host "Selected SerialNumber"
  7.         }
  8.     }
This topic is 7 years and 3 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