Get Tag from ToolStripMenuItem

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 1 year and 10 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
gitpowershelluser
Posts: 24
Last visit: Mon Feb 06, 2023 11:30 am
Has voted: 1 time

Get Tag from ToolStripMenuItem

Post by gitpowershelluser »

In my PSF, I load the dropdown items like this and my csv looks like this
Get-PDFFiles, Invoke_GetFiles
  1.  
  2.         $Collection = "\\$Server\Instructions.CSV"
  3.         $nstructions = Import-Csv $Collection -Delimiter ',' -Header @("Name", "Value")
  4.         foreach ($item in $Instructions)
  5.         {
  6.             #Adding the MenuItem
  7.             $MenuItem = New-Object System.Windows.Forms.ToolStripMenuItem
  8.             $MenuItem.Text = $item.Name
  9.             $MenuItem.Tag = $item.Value
  10.             $CustomInstructions.DropDownItems.Add($MenuItem)
  11.         }
This successfully loads but I cannot get the Tag property from the dropdownitemclicked event, just the text of the item I clicked
by jvierra » Mon May 02, 2022 8:53 pm
You need to specify the "Tag" property.
$Name = $_.ClickedItem.Tag
Go to full post
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Get Tag from ToolStripMenuItem

Post by jvierra »

How are you trying to get the "Tag"?
gitpowershelluser
Posts: 24
Last visit: Mon Feb 06, 2023 11:30 am
Has voted: 1 time

Re: Get Tag from ToolStripMenuItem

Post by gitpowershelluser »

$MyToolStrip_DropDownItemClicked = [System.Windows.Forms.ToolStripItemClickedEventHandler]{
$Name = $_.ClickedItem
}

Silly of me to think all of the properties would be carried over
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Get Tag from ToolStripMenuItem

Post by jvierra »

You need to specify the "Tag" property.
$Name = $_.ClickedItem.Tag
This topic is 1 year and 10 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