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.
Locked
User avatar
Cmajda
Posts: 4
Joined: Wed May 06, 2015 2:12 am

automatic generate ToolStripMenuItem and add Click action

Post by Cmajda » Mon Nov 05, 2018 3:33 am

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"
}

User avatar
jvierra
Posts: 13147
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: automatic generate ToolStripMenuItem and add Click action

Post by jvierra » Mon Nov 05, 2018 3:47 am

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
Joined: Wed May 06, 2015 2:12 am

Re: automatic generate ToolStripMenuItem and add Click action

Post by Cmajda » Mon Nov 05, 2018 3:52 am

Here is project
Dynamicmenu.zip
(61.04 KiB) Downloaded 12 times

User avatar
jvierra
Posts: 13147
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: automatic generate ToolStripMenuItem and add Click action

Post by jvierra » Mon Nov 05, 2018 4:08 am

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.

User avatar
jvierra
Posts: 13147
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: automatic generate ToolStripMenuItem and add Click action

Post by jvierra » Mon Nov 05, 2018 4:24 am

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 11 times

User avatar
Cmajda
Posts: 4
Joined: Wed May 06, 2015 2:12 am

Re: automatic generate ToolStripMenuItem and add Click action

Post by Cmajda » Mon Nov 05, 2018 6:14 am

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 11 times

User avatar
jvierra
Posts: 13147
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: automatic generate ToolStripMenuItem and add Click action

Post by jvierra » Mon Nov 05, 2018 2:06 pm

Please post the PSF file and not an exported ps1 file.

User avatar
Cmajda
Posts: 4
Joined: Wed May 06, 2015 2:12 am

Re: automatic generate ToolStripMenuItem and add Click action

Post by Cmajda » Mon Nov 05, 2018 10:41 pm

DynamicMenu.psf
(50.66 KiB) Downloaded 10 times

User avatar
jvierra
Posts: 13147
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: automatic generate ToolStripMenuItem and add Click action

Post by jvierra » Mon Nov 05, 2018 11:01 pm

Start by looking at how these menu items are created and how they work with events.
Attachments
DynamicMenu.psf
(35.02 KiB) Downloaded 13 times

Locked