automatic generate ToolStripMenuItem and add Click action

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 5 years and 4 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
Cmajda
Posts: 4
Last visit: Tue Nov 20, 2018 5:26 am

automatic generate ToolStripMenuItem and add Click action

Post by Cmajda »

Hello,
I created new form projects /Form projects /empty form
mainForm context this code

Goal of script is automatic generate items(PC) in toolsstripsplitbutton and for each item(PC) after click, replace text of label for text of selected item (pc)

For example:
Click to item 1 with text PC1 replace text of label to PC1

in my case when a click on item(pc1) every time text of label is replacet to PC3
Where is problem?

Thenk you

Code: Select all

$script:PCList = "PC1", "PC2", "PC3"
$script:RefreshToolStripButtonListOfPC = "Yes"

function listPC ($position, $value)
{
	$global:value = $value
	$ToolStripButtonListOfPC.DropDown.Items[$position].Add_click({ $LABEL_ShowPC.Text = $($value)} )
}

function listPC2 ($position, $value)
{
	$global:value = $value
	$item2 = New-Object System.Windows.Forms.ToolStripMenuItem
	$item2.Text = $value
	$ToolStripButtonListOfPC.DropDown.Items.Add($item2)
	$sb =[scriptblock]::Create({$LABEL_ShowPC.Text = "$global:value"})
	$item2.add_Click($sb)
}

$button1_Click={
	$LABEL_ShowPC.Text = "NOT SET Button"
}

$ShowItem = {
	if ($RefreshToolStripButtonListOfPC -like "Yes")
	{

		[int]$i = 1
		foreach ($pc in $PCList)
		{
			#$ToolStripButtonListOfPC.DropDown.Items.Add("&$pc")
			listPC2 -position $i -value $pc
			$i++
		}
		$script:RefreshToolStripButtonListOfPC = "No"
	}
}
$ladaToolStripMenuItem_Click={
	$LABEL_ShowPC.Text = "FIX_PC"
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: automatic generate ToolStripMenuItem and add Click action

Post by jvierra »

Without the PSF file it is not possible to decode what you are trying to do. Also some of you code is not used anywhere.
Note that collections are zero based in PowerShell.
User avatar
Cmajda
Posts: 4
Last visit: Tue Nov 20, 2018 5:26 am

Re: automatic generate ToolStripMenuItem and add Click action

Post by Cmajda »

Here is project
Dynamicmenu.zip
(61.04 KiB) Downloaded 128 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: automatic generate ToolStripMenuItem and add Click action

Post by jvierra »

None of your code is ever used.

I recommend just using a PSF until you learn enough about both PowerShell and Windows Forms to understand how to code this.

What is the purpose of this form? If you can clearly describe what event happens and what that event is supposed to do and why you would be on track to start coding your solution. As it is you seem to just be pasting pieces of code that are never used. All code must be executed in an event. The event must happen. What you uploaded has one event, the "click" event which does nothing but display test in a label.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: automatic generate ToolStripMenuItem and add Click action

Post by jvierra »

Here is an example to get you started. Just run the attached PS file without project and try to understand how Forms and PS work to do things with very little code.
Attachments
MainForm.psf
(23.07 KiB) Downloaded 99 times
User avatar
Cmajda
Posts: 4
Last visit: Tue Nov 20, 2018 5:26 am

Re: automatic generate ToolStripMenuItem and add Click action

Post by Cmajda »

core code in powershell.
The problem is in function "function addItemToToolStripMenuItem" on line number 119
Is it more understandable where do I have a problem?
Every time is text of label1 set to PC -3 except PC -0 (text is set outsite function)
Attachments
dynamicDropDownMenu.ps1
(10.11 KiB) Downloaded 109 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: automatic generate ToolStripMenuItem and add Click action

Post by jvierra »

Please post the PSF file and not an exported ps1 file.
User avatar
Cmajda
Posts: 4
Last visit: Tue Nov 20, 2018 5:26 am

Re: automatic generate ToolStripMenuItem and add Click action

Post by Cmajda »

DynamicMenu.psf
(50.66 KiB) Downloaded 137 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: automatic generate ToolStripMenuItem and add Click action

Post by jvierra »

Start by looking at how these menu items are created and how they work with events.
Attachments
DynamicMenu.psf
(35.02 KiB) Downloaded 120 times
This topic is 5 years and 4 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