powershell form picture box function not working - forms

I have the following code that I can run outside of a function and it works just fine.
I am trying to make a form that will be displaying multiple images so I need to format it into a function.
Add-Type -AssemblyName System.Windows.Forms, System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.StartPosition = "CenterScreen"
$Form.Size = New-Object System.Drawing.Point(800,800)
$Form.text = "Example Form"
$Form.TopMost = $false
$Form.MaximumSize = $Form.Size
$Form.MinimumSize = $Form.Size
$Picture = (get-item ("C:\Users\User\Desktop\t.png"))
$img = [System.Drawing.Image]::Fromfile($Picture)
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Width = $img.Size.Width
$pictureBox.Height = $img.Size.Height
$pictureBox.Image = $img
$pictureBox.Location = [Drawing.Point]::new(15, 15)
$Form.controls.add($pictureBox)
$Form.ShowDialog()
but when I tried to assemble this as a function it is not working
Add-Type -AssemblyName System.Windows.Forms, System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.StartPosition = "CenterScreen"
$Form.Size = New-Object System.Drawing.Point(800,800)
$Form.text = "Example Form"
$Form.TopMost = $false
$Form.MaximumSize = $Form.Size
$Form.MinimumSize = $Form.Size
function pictureBox {
[CmdletBinding()]
param (
[Parameter (Mandatory = $True)]
[string] $p,
[Parameter (Mandatory = $True)]
[int] $lx,
[Parameter (Mandatory = $True)]
[int] $ly
)
$Picture = (get-item ($p))
$img = [System.Drawing.Image]::Fromfile($Picture)
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Size = [Drawing.Size]::new($img.Size.Width,$img.Size.Height)
$pictureBox.Location = [Drawing.Point]::new($lx, $ly)
$pictureBox.Image = $img
}
$v1 = pictureBox -p "C:\Users\User\Desktop\t.png" -lx 15 -ly 15
$Form.controls.add($v1)
$Form.ShowDialog()
What am I doing wrong?

Your function pictureBox is not returning anything, it creates a new PictureBox instance, updates it's properties but then that object is never outputted, hence $v1 gets assigned AutomationNull.Value.
# Same code for creating the Form here
function pictureBox {
[CmdletBinding()]
param (
[Parameter (Mandatory = $True)]
[string] $p,
[Parameter (Mandatory = $True)]
[int] $lx,
[Parameter (Mandatory = $True)]
[int] $ly
)
$img = [Drawing.Image]::Fromfile($PSCmdlet.GetUnresolvedProviderPathFromPSPath($p))
[Windows.Forms.PictureBox]#{
Size = [Drawing.Size]::new($img.Size.Width, $img.Size.Height)
Location = [Drawing.Point]::new($lx, $ly)
Image = $img
}
}
$v1 = pictureBox -p "C:\Users\User\Desktop\t.png" -lx 15 -ly 15
$Form.controls.add($v1)
$Form.ShowDialog()

Related

How to make a textbox function in a form?

I have an application I am making with multiple text boxes and I am trying to clean it up by making a text box function. However the name parameter itself needs to return a variable name and I am just not quite sure how to do that. I tried giving the parameter the
[psvariable] type but that does not seem to be working.
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.StartPosition = "CenterScreen"
$Form.Size = New-Object System.Drawing.Point(415,838)
$Form.text = "Test Form"
$Form.TopMost = $false
$Form.MaximumSize = $Form.Size
$Form.MinimumSize = $Form.Size
function textBox {
[CmdletBinding()]
param (
[Parameter (Mandatory = $True)]
[psvariable]$name,
[Parameter (Mandatory = $True)]
[string]$ml,
[Parameter (Mandatory = $True)]
[int]$lx,
[Parameter (Mandatory = $True)]
[int]$ly,
[Parameter (Mandatory = $True)]
[string]$text
)
$name = New-Object system.Windows.Forms.TextBox
$name.multiline = $ml
$name.Size = New-Object System.Drawing.Size(300,60)
$name.location = New-Object System.Drawing.Point($lx,$ly)
$name.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$name.Text = $text
}
textBox -name '$source' -ml $True -lx 15 -ly 100 -text "test it"
$Form.controls.AddRange(#($source))
[void]$Form.ShowDialog()
exit
It's unclear why is the $name parameter there in your function to begin with, I don't see any use for it. The other problem is that your function is not returning anything and your function invocation is not capturing anything either.
Add-Type -AssemblyName System.Windows.Forms, System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
# Form code here
function textBox {
[CmdletBinding()]
param (
[Parameter (Mandatory = $True)]
[bool] $ml,
[Parameter (Mandatory = $True)]
[int] $lx,
[Parameter (Mandatory = $True)]
[int] $ly,
[Parameter (Mandatory = $True)]
[string] $text
)
[Windows.Forms.TextBox]#{
Multiline = $ml
Size = [Drawing.Size]::new(300,60)
Location = [Drawing.Point]::new($lx, $ly)
Font = [Drawing.Font]::new('Microsoft Sans Serif', 10)
Text = $text
}
}
# Capture here
$txtBox = textBox -ml $True -lx 15 -ly 100 -text "test it"
$Form.Controls.AddRange(#($txtBox))
[void] $Form.ShowDialog()
To clarify further, the $Name Parameter was constraint to be a PSVariable:
[psvariable] $Name = $null
Hence when, in your function's body, you try to assign an instance different than a PSVariable:
$name = New-Object System.Windows.Forms.TextBox
You would receive this error, which is basically telling you that PowerShell cannot convert an instance of TextBox to the constraint type:
Cannot convert the "System.Windows.Forms.TextBox, Text: " value of type "System.Windows.Forms.TextBox" to type "System.Management.Automation.PSVariable".

How to add_click to a pictureBox in a function in a powershell form?

I am tring to make a form with several images that will open a URL.
Is it possible to add the click event to the function itself or do I have to add it after the function has been called?
Add-Type -AssemblyName System.Windows.Forms, System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.StartPosition = "CenterScreen"
$Form.Size = New-Object System.Drawing.Point(800,800)
$Form.text = "Example Form"
$Form.TopMost = $false
$Form.MaximumSize = $Form.Size
$Form.MinimumSize = $Form.Size
$Form.BackColor = "#000000"
function pictureBox {
[CmdletBinding()]
param (
[Parameter (Mandatory = $True)]
[string] $p,
[Parameter (Mandatory = $True)]
[int] $lx,
[Parameter (Mandatory = $True)]
[int] $ly,
[Parameter (Mandatory = $True)]
[string] $url
)
$img = [Drawing.Image]::Fromfile($PSCmdlet.GetUnresolvedProviderPathFromPSPath($p))
[Windows.Forms.PictureBox]#{
Size = [Drawing.Size]::new($img.Size.Width, $img.Size.Height)
Location = [Drawing.Point]::new($lx, $ly)
Image = $img
}
Add_Click ( { start-process $url } )
}
$v1 = pictureBox -p "C:\Users\User\Desktop\t.png" -lx 20 -ly 65 -url **THE-URL**
$v1.Add_Click( { start-process **THE-URL** } )
It's possible to do it from the function itself, first you need to instantiate the PictureBox, in the example below, the object is stored in $pbx. After that you can add the Click event to it.
As you may note there is also a GetNewClosure() method invocation, in this case it is required because the ScriptBlock needs to "remember" the value of $url.
function pictureBox {
[CmdletBinding()]
param (
[Parameter (Mandatory = $True)]
[string] $p,
[Parameter (Mandatory = $True)]
[int] $lx,
[Parameter (Mandatory = $True)]
[int] $ly,
[Parameter (Mandatory = $True)]
[string] $url
)
$img = [Drawing.Image]::Fromfile($PSCmdlet.GetUnresolvedProviderPathFromPSPath($p))
$pbx = [Windows.Forms.PictureBox]#{
Size = [Drawing.Size]::new($img.Size.Width, $img.Size.Height)
Location = [Drawing.Point]::new($lx, $ly)
Image = $img
}
$pbx.Add_Click({ Start-Process $url }.GetNewClosure())
$pbx
}
$v1 = pictureBox -p "C:\Users\User\Desktop\t.png" -lx 20 -ly 65 -url "https://stackoverflow.com/"
$Form.Controls.Add($v1)
$Form.ShowDialog()

Powershell GUI tabbed layout script placement

I wrote six small gui scripts that I'd like to place in a single file, for the sake of converting said file into rxecutable with ps2exe.
I've found a script ,here on stack that is perfect for what I want. Unfortunatelly I cann't find any info on script placement within tabs and MS documentation leads me to ISE tabs, which is not helpfull.
Say I'd like to place this
Add-Type -AssemblyName PresentationFramework
$button_click = {
$folder = $textBox1.Text;
$pytanie = [System.Windows.MessageBox]::Show('Czy chcesz usunac folder?', '', '4');
If($pytanie -eq 'Yes')
{Remove-Item –path $folder –recurse -Force};
$test = Test-Path $folder;
if ($test -eq $false){[System.Windows.MessageBox]::Show('Folder Usuniety', '', '0')}}
$label2 = New-Object System.Windows.Forms.Label
$label2.AutoSize = $True
$label2.Text = ("Scieżka")
$label2.Location = New-Object System.Drawing.Point (10,30)
$label2.Size = New-Object System.Drawing.Size (25,70)
$label2.Font = [System.Drawing.Font]::new("Arial", 10, [System.Drawing.FontStyle]::Bold)
$textBox1 = New-Object System.Windows.Forms.TextBox
$textBox1.Location = New-Object System.Drawing.Point(10,70) ### Location of the text box
$textBox1.Size = New-Object System.Drawing.Size(200,50) ### Size of the text box
$textBox1.Multiline = $false ### Allows multiple lines of data
$textBox1.Font = New-Object System.Drawing.Font("Consolas",10,[System.Drawing.FontStyle]::Regular)
$textBox1.ReadOnly=$false
$button = New-Object System.Windows.Forms.Button
$button.Location = New-Object System.Drawing.Point(10,120)
$button.Size = New-Object System.Drawing.Size (200,30)
$button.Text = "Usun Folder"
$button.Add_Click($button_click)
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Mapowanie' ### Text to be displayed in the title
$form.Size = New-Object System.Drawing.Size(240,200) ### Size of the window
$form.StartPosition = 'Manual'
$form.Location = '10,10'
$form.Topmost = $true ### Optional - Opens on top of other windows
$form.Controls.AddRange(#($textBox1,$button, $label2))
$form.ShowDialog()
within a tab. How to do it?
I think that it the
$Tab1.Controls.Add($button)
You are looking for.
For example
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$ApplicationForm = New-Object System.Windows.Forms.Form
$ApplicationForm.StartPosition = "CenterScreen"
$ApplicationForm.Topmost = $false
$ApplicationForm.Size = "800,600"
$FormTabControl = New-object System.Windows.Forms.TabControl
$FormTabControl.Size = "755,475"
$FormTabControl.Location = "25,75"
$ApplicationForm.Controls.Add($FormTabControl)
$Tab1 = New-object System.Windows.Forms.Tabpage
$Tab1.DataBindings.DefaultDataSourceUpdateMode = 0
$Tab1.UseVisualStyleBackColor = $True
$Tab1.Name = "Tab1"
$Tab1.Text = "Tab1”
$FormTabControl.Controls.Add($Tab1)
$textBox1 = New-Object System.Windows.Forms.TextBox
$textBox1.Location = New-Object System.Drawing.Point(10,70) ### Location of the text box
$textBox1.Size = New-Object System.Drawing.Size(200,50) ### Size of the text box
$textBox1.Multiline = $false ### Allows multiple lines of data
$textBox1.Font = New-Object System.Drawing.Font("Consolas",10,[System.Drawing.FontStyle]::Regular)
$textBox1.ReadOnly=$false
$Tab1.Controls.Add($textBox1)
$Tab2 = New-object System.Windows.Forms.Tabpage
$Tab2.DataBindings.DefaultDataSourceUpdateMode = 0
$Tab2.UseVisualStyleBackColor = $True
$Tab2.Name = "Tab2"
$Tab2.Text = "Tab2”
$FormTabControl.Controls.Add($Tab2)
$button = New-Object System.Windows.Forms.Button
$button.Location = New-Object System.Drawing.Point(10,120)
$button.Size = New-Object System.Drawing.Size (200,30)
$button.Text = "Usun Folder"
$button.Add_Click($button_click)
$Tab2.Controls.Add($button)
# Initlize the form
$ApplicationForm.Add_Shown({$ApplicationForm.Activate()})
[void] $ApplicationForm.ShowDialog()

Getting keypress on a Form in Powershell

Im trying to make a image viewer in powershell using some windows forms libraries, I can already store the images location inside an array, but now I want it to detect the keys Right and Left to change between the locations inside the array
Here is the code I'm using:
param(
[parameter (Mandatory=$false, position=0, ParameterSetName='url')]
[string]$url = ''
)
Add-Type -AssemblyName 'System.Windows.Forms'
[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.Application]::EnableVisualStyles()
Function Load-Images{
param (
[parameter (Mandatory=$true, position=0, ParameterSetName='path')]
$path
)
$screen = [System.Windows.Forms.Screen]::AllScreens
$form = new-object Windows.Forms.Form
$form.Text = "Image Viewer"
$form.Size = New-Object System.Drawing.Size($screen.WorkingArea[0].Width, $screen.WorkingArea[0].Height)
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Size = New-Object System.Drawing.Size($screen.WorkingArea[0].Width, $screen.WorkingArea[0].Height)
$pictureBox.Image = $path
$pictureBox.SizeMode = 'Zoom'
$pictureBox.Anchor = 'Top,Left,Bottom,Right'
$form.controls.add($pictureBox)
$form.Add_Shown( { $form.Activate() } )
$form.ShowDialog()
$img.dispose()
}
if(!$url) {
$dir = Get-Location
$Files = #(Get-ChildItem "$($dir.Path)\*" -Include *.jpg, *.jpeg, *.png)
$maxSize = $Files.Length
Write-Output $maxSize
Write-Output $Files.Fullname
$img = [System.Drawing.Image]::Fromfile((Get-Item $Files.Fullname[0]))
Load-Images -path $img
}
Continuing from my comment. What you are after is not specific to PowerShell at all. It's a UX/UI design/property/event item.
For Example, here is one showing adding and using the property/event setting to watch for defined keypresses. Just run the function, call the function, and hit 'Enter' or 'Esc', or click 'OK' to fire those events.
function Start-CreateForm
{
#Import Assemblies
Add-Type -AssemblyName System.Windows.Forms,
System.Drawing
$Form1 = New-Object System.Windows.Forms.Form
$OKButton = New-Object System.Windows.Forms.Button
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
$Label1 = New-Object System.Windows.Forms.Label
$textBox1 = New-Object System.Windows.Forms.TextBox
$Field1 = ""
# Check for ENTER and ESC presses
$Form1.KeyPreview = $True
$Form1.Add_KeyDown({if ($PSItem.KeyCode -eq "Enter")
{
# if enter, perform click
$OKButton.PerformClick()
}
})
$Form1.Add_KeyDown({if ($PSItem.KeyCode -eq "Escape")
{
# if escape, exit
$Form1.Close()
}
})
# The action on the button
$handler_OK_Button_Click=
{
$Field1 = $textBox1.Text
$Field1
# Returns a message of no data
if ($Field1 -eq "") {[System.Windows.Forms.MessageBox]::Show("You didn't enter anything!", "Data")}
# Returns what they types. You could add your code here
else {[System.Windows.Forms.MessageBox]::Show($Field1, "Data")}
}
$OnLoadForm_StateCorrection=
{
$Form1.WindowState = $InitialFormWindowState
}
# Form Code
$Form1.Name = "Data_Form"
$Form1.Text = "Data Form"
$Form1.MaximizeBox = $false #lock form
$Form1.FormBorderStyle = 'Fixed3D'
# None,FixedDialog,FixedSingle,FixedToolWindow,Sizable,SizableToolWindow
# Icon
$Form1.Icon = [Drawing.Icon]::ExtractAssociatedIcon((Get-Command powershell).Path)
# $NotifyIcon.Icon = [Drawing.Icon]::ExtractAssociatedIcon((Get-Command powershell).Path)
$Form1.DataBindings.DefaultDataSourceUpdateMode = 0
$Form1.StartPosition = "CenterScreen"# moves form to center of screen
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 300 # sets X
$System_Drawing_Size.Height = 150 # sets Y
$Form1.ClientSize = $System_Drawing_Size
$OKButton.Name = "OK_Button"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 45
$System_Drawing_Size.Height = 23
$OKButton.Size = $System_Drawing_Size
$OKButton.UseVisualStyleBackColor = $True
$OKButton.Text = "OK"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 30
$System_Drawing_Point.Y = 113
$OKButton.Location = $System_Drawing_Point
$OKButton.DataBindings.DefaultDataSourceUpdateMode = 0
$OKButton.add_Click($handler_OK_Button_Click)
$Form1.Controls.Add($OKButton)
$InitialFormWindowState = $Form1.WindowState
$Form1.add_Load($OnLoadForm_StateCorrection)
$Label1.Location = New-Object System.Drawing.Point(10,20)
$Label1.Size = New-Object System.Drawing.Size(280,20)
$Label1.Text = "Enter data here:"
$Form1.Controls.Add($Label1)
$textBox1.TabIndex = 0 # Places cursor in field
$textBox1.Location = New-Object System.Drawing.Point(10,40)
$textBox1.Size = New-Object System.Drawing.Size(260,20)
$Form1.Controls.Add($textBox1)
$Form1.Topmost = $True # Moves form to top and stays on top
$Form1.Add_Shown({$textBox1.Select()})
# Show Form
$Form1.ShowDialog()
}
For your use case, you just have to use the needed keyboard specifics.

Can I make a parameter set depend on the value of another parameter?

Let's say I have a function like:
function Authenticate
{
param
(
[ValidateSet('WindowsAuthentication','UsernameAndPassword')][string] $AuthenticationType,
[Parameter(ParameterSetName='Set1')][string] $Username,
[Parameter(ParameterSetName='Set1')][string] $Password
)
..
}
And I would like to make the parameter set to be mandatory when $AuthenticationType = 'UsernameAndPassword' but also so that it cannot be used if $AuthenticationType = 'WindowsAuthentication'.
It this even possible in PowerShell?
Using the link from Tim Ferrill's answer, I created the following function to help create dynamic parameters:
function New-DynamicParameter
{
[CmdletBinding(DefaultParameterSetName = 'Core')]
param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)][string] $Name,
[Parameter(Mandatory = $true, ParameterSetName = 'Core')][Parameter(Mandatory = $true, ParameterSetName = 'ValidateSet')][type] $Type,
[Parameter(Mandatory = $false)][string] $ParameterSetName = '__AllParameterSets',
[Parameter(Mandatory = $false)][bool] $Mandatory = $false,
[Parameter(Mandatory = $false)][int] $Position,
[Parameter(Mandatory = $false)][bool] $ValueFromPipelineByPropertyName = $false,
[Parameter(Mandatory = $false)][string] $HelpMessage,
[Parameter(Mandatory = $true, ParameterSetName = 'ValidateSet')][string[]] $ValidateSet,
[Parameter(Mandatory = $false, ParameterSetName = 'ValidateSet')][bool] $IgnoreCase = $true
)
process
{
# Define Parameter Attributes
$ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
$ParameterAttribute.ParameterSetName = $ParameterSetName
$ParameterAttribute.Mandatory = $Mandatory
$ParameterAttribute.Position = $Position
$ParameterAttribute.ValueFromPipelineByPropertyName = $ValueFromPipelineByPropertyName
$ParameterAttribute.HelpMessage = $HelpMessage
# Define Parameter Validation Options if ValidateSet set was used
if ($PSCmdlet.ParameterSetName -eq 'ValidateSet')
{
$ParameterValidateSet = New-Object System.Management.Automation.ValidateSetAttribute -ArgumentList $ValidateSet -Strict (!$IgnoreCase)
}
# Add Parameter Attributes and ValidateSet to an Attribute Collection
$AttributeCollection = New-Object Collections.ObjectModel.Collection[System.Attribute]
$AttributeCollection.Add($ParameterAttribute)
$AttributeCollection.Add($ParameterValidateSet)
# Add parameter to parameter list
$Parameter = New-Object System.Management.Automation.RuntimeDefinedParameter -ArgumentList #($Name, $Type, $AttributeCollection)
# Expose parameter to the namespace
$ParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$ParameterDictionary.Add($Name, $Parameter)
return $ParameterDictionary
}
}
And solved my particular problem in the following way:
function Authenticate
{
param
(
[ValidateSet('WindowsAuthentication','UsernameAndPassword')][string] $AuthenticationType,
)
DynamicParam
{
if ($AuthenticationType -eq 'UsernameAndPassword')
{
New-DynamicParameter Username [string] -Mandatory $true
New-DynamicParameter Password [string] -Mandatory $true
}
}
...
}
It became unneeded to have a parameter set when using Dynamic Parameter so I removed the parameter set.
You can do this using DynamicParam. I saw a decent post on this recently here.
DynamicParam {
if ($AuthenticationType -eq 'UsernameAndPassword') {
#create ParameterAttribute Objects for the username and password
$unAttribute = New-Object System.Management.Automation.ParameterAttribute
$unAttribute.Mandatory = $true
$unAttribute.HelpMessage = "Please enter your username:"
$pwAttribute = New-Object System.Management.Automation.ParameterAttribute
$pwAttribute.Mandatory = $true
$pwAttribute.HelpMessage = "Please enter a password:"
#create an attributecollection object for the attributes we just created.
$attributeCollection = new-object System.Collections.ObjectModel.Collection[System.Attribute]
#add our custom attributes
$attributeCollection.Add($unAttribute)
$attributeCollection.Add($pwAttribute)
#add our paramater specifying the attribute collection
$unParam = New-Object System.Management.Automation.RuntimeDefinedParameter('username', [string], $attributeCollection)
$pwParam = New-Object System.Management.Automation.RuntimeDefinedParameter('password', [string], $attributeCollection)
#expose the name of our parameter
$paramDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$paramDictionary.Add('username', $unParam)
$paramDictionary.Add('password', $pwParam)
return $paramDictionary
}
}
Process {
$PSBoundParameters.username
$PSBoundParameters.password
}