Here is what I'm trying to do:
I'm creating a form with a MenuStrip. One of the MenuStripItems is $Env:UserName. The DropDownItems of that MenuStripItem would need to be dynamically created, based on a list of files (GCI "C:\Folder" -File)
Here is what I have:
I'm creating a form with a MenuStrip. One of the MenuStripItems is $Env:UserName. The DropDownItems of that MenuStripItem would need to be dynamically created, based on a list of files (GCI "C:\Folder" -File)
Here is what I have:
Add-Type-AssemblyNameSystem.Windows.Forms
Add-Type-AssemblyNameSystem.Drawing
$Form=New-ObjectSystem.Windows.Forms.Form
$Form.Width= 300
$Form.Height= 200
$Form.Text= ("Test Form")
$Form.StartPosition="CenterScreen"
#Define MenuBar
$MenuBar=New-ObjectSystem.Windows.Forms.MenuStrip
$Form.Controls.Add($MenuBar)
#Define UserMenu
$UserMenu=New-ObjectSystem.Windows.Forms.ToolStripMenuItem
$UserMenu.Text=$Env:USERNAME
$MenuBar.Items.Add($UserMenu)
#Create UserFolder, based on Environment Variable
$UserFolder= ("C:\Scripts\$Env:USERNAME")
If(-not (Test-Path$UserFolder)){
$UserFolder=New-Item-ItemType Directory -Path$UserFolder
}
#Create MenuItem for each File Found in UserFolder
ForEach($Filein (Get-ChildItem$UserFolder-Recurse-File)){
$MenuItem=$UserMenu.DropDownItems.Add($File.Name)
}
$Form.ShowDialog()
Add-Type-AssemblyNameSystem.Drawing
$Form=New-ObjectSystem.Windows.Forms.Form
$Form.Width= 300
$Form.Height= 200
$Form.Text= ("Test Form")
$Form.StartPosition="CenterScreen"
#Define MenuBar
$MenuBar=New-ObjectSystem.Windows.Forms.MenuStrip
$Form.Controls.Add($MenuBar)
#Define UserMenu
$UserMenu=New-ObjectSystem.Windows.Forms.ToolStripMenuItem
$UserMenu.Text=$Env:USERNAME
$MenuBar.Items.Add($UserMenu)
#Create UserFolder, based on Environment Variable
$UserFolder= ("C:\Scripts\$Env:USERNAME")
If(-not (Test-Path$UserFolder)){
$UserFolder=New-Item-ItemType Directory -Path$UserFolder
}
#Create MenuItem for each File Found in UserFolder
ForEach($Filein (Get-ChildItem$UserFolder-Recurse-File)){
$MenuItem=$UserMenu.DropDownItems.Add($File.Name)
}
$Form.ShowDialog()
Now, I want to add $MenuItem.Add_Click to each, that will launch the file that the menu item references, like this (replacing the entire ForEach) - something to this effect:
#Create MenuItem for each File Found in UserFolder
ForEach($Filein (Get-ChildItem$UserFolder-Recurse-File)){
#Adding the MenuItem
$MenuItem=$UserMenu.DropDownItems.Add($File.Name)
#Add Add_Click to each MenuItem, which would execute the file.
$MenuItem.Add_Click({
Invoke-Item ("C:\Scripts\"+$Env:USERNAME+"\"+$File.Name)
})
}
ForEach($Filein (Get-ChildItem$UserFolder-Recurse-File)){
#Adding the MenuItem
$MenuItem=$UserMenu.DropDownItems.Add($File.Name)
#Add Add_Click to each MenuItem, which would execute the file.
$MenuItem.Add_Click({
Invoke-Item ("C:\Scripts\"+$Env:USERNAME+"\"+$File.Name)
})
}
I have tried creating and executing a seperate function inside the Add_Click, and disposing of the $MenuItem variable at the end of the function, to no avail.
I'm going crazy here...please help!
-Matthew