Powershell Get values from nested arrays for Textboxes - arrays

I'm trying to create a gui with comboboxes and textboxes. If the user select a item from the Combobox, the related values from an array should appears in textboxes. Can someone give me a clue? I already tried to enumerat thru the arrays and select the key/value pair with -eq, but unfortunately I was not able to solve it this way.
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
#Arrays
$CB_NDL_Array = #{
City1 = #{
Street = "Test1"
Postalcode = "5435"
}
City2 = #{
Street="Test2"
Postalcode="23423"
}
City3 = #{
Street="Test3"
Postalcode="234"
}}
$UserManager.controls.AddRange(#($Vorname,$Nachname,$TB_Givenname,$TB_Surname,$Anmeldename,$TB_SamAccountName,$Passwort,$TB_Password,$Niederlassung,$CB_NDL,$Street,$TB_Streetaddress,$City,$TB_City,$PLZ,$TB_Postalcode,$Buero,$TB_Office,$Abteilung,$TB_Department,$Position,$CB_Title,$Firma,$CB_Company,$Manager,$TB_Manager,$Telefon,$TB_OfficePhone,$Mobile,$TB_Mobile,$UserInfo,$Exchange,$RB_exc_yes,$RB_exc_no,$Email_address,$TB_email_address,$CB_Database,$Database,$B_Create))
$CB_NDL = New-Object system.Windows.Forms.ComboBox
$CB_NDL.text = ""
$CB_NDL.width = 100
$CB_NDL.height = 20
$CB_NDL.location = New-Object System.Drawing.Point(110,180)
$CB_NDL.Font = 'Microsoft Sans Serif,10'
$CB_NDL.SelectedItem
$Street = New-Object system.Windows.Forms.Label
$Street.text = "Strasse"
$Street.AutoSize = $true
$Street.width = 25
$Street.height = 10
$Street.location = New-Object System.Drawing.Point(15,210)
$Street.Font = 'Microsoft Sans Serif,10'
$TB_Streetaddress = New-Object system.Windows.Forms.TextBox
$TB_Streetaddress.multiline = $false
$TB_Streetaddress.width = 100
$TB_Streetaddress.height = 20
$TB_Streetaddress.location = New-Object System.Drawing.Point(110,210)
$TB_Streetaddress.Font = 'Microsoft Sans Serif,10'
$City = New-Object system.Windows.Forms.Label
$City.text = "Ort"
$City.AutoSize = $true
$City.width = 25
$City.height = 10
$City.location = New-Object System.Drawing.Point(15,240)
$City.Font = 'Microsoft Sans Serif,10'
$TB_City = New-Object system.Windows.Forms.TextBox
$TB_City.multiline = $false
$TB_City.width = 100
$TB_City.height = 20
$TB_City.location = New-Object System.Drawing.Point(110,240)
$TB_City.Font = 'Microsoft Sans Serif,10'
$PLZ = New-Object system.Windows.Forms.Label
$PLZ.text = "PLZ"
$PLZ.AutoSize = $true
$PLZ.width = 25
$PLZ.height = 10
$PLZ.location = New-Object System.Drawing.Point(15,270)
$PLZ.Font = 'Microsoft Sans Serif,10'
$TB_Postalcode = New-Object system.Windows.Forms.TextBox
$TB_Postalcode.multiline = $false
$TB_Postalcode.width = 100
$TB_Postalcode.height = 20
$TB_Postalcode.location = New-Object System.Drawing.Point(110,270)
$TB_Postalcode.Font = 'Microsoft Sans Serif,10'
$CB_NDL.Add_SelectedIndexChanged({
$TB_City.text = $CB_NDL.SelectedItem
$TB_Postalcode.text = $CB_NDL_Array.values | Where-Object{$CB_NDL.SelectedItem} | Where CB_NDL_Array.$_.key -eq "Postalcode" })
foreach ($NDL in $CB_NDL_Array.keys){
$CB_NDL.items.AddRange("$NDL")
}
foreach ($Title in $CB_Title_Array){
$CB_Title.items.add("$Title")
}
foreach ($Company in $CB_Company_Array){
$CB_Company.items.add("$Company")
}
[void]$UserManager.ShowDialog()

$SelectedItem=$CB_NDL.SelectedItem
$TB_Postalcode.text = $CB_NDL_Array.$SelectedItem.Postalcode

Related

how to create dynamic layout since AutoSize in winforms doesn't work?

trying to create simple win form in PowerShell. there will be some automatically calculated checkbox and i'm struggling with sizes - seems that 'AutoSize' is a bullsh*t and values returned are from nowhere. can anyone please help and suggest how you create dynamic positioning? based on which values?
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$nrOfNIC = 2
$nrOfDisks = 3
$nrOfPIP = 1
$vShift = 20
$allChkb = 1
$chkForm = New-Object system.Windows.Forms.Form
$chkForm.text = "Remove Resources"
$chkForm.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10)
$chkForm.AutoSize = $true
$chkForm.StartPosition = 'CenterScreen'
$chkForm.FormBorderStyle = 'Fixed3D'
$chkForm.Icon = [System.Drawing.SystemIcons]::Question
$chkForm.Topmost = $true
$chkForm.MaximizeBox = $false
$chkVMBox = new-object System.Windows.Forms.GroupBox
#$chkVMBox.MinimumSize = New-Object System.Drawing.Size(180,100)
$chkVMBox.AutoSize = $true
$chkVMBox.Location = New-Object System.Drawing.Point(10,10)
$chkVMBox.Text = 'VM resources'
#$chkVMBox.Anchor = 'left,top'
#$lastControl = $chkVMBox
if($nrOfDisks -gt 0) {
$chkVMDisks = new-object System.Windows.Forms.GroupBox
#$chkVMDisks.MinimumSize = New-Object System.Drawing.Size(180,20)
$chkVMDisks.Location = New-Object System.Drawing.Point(10,20)
$chkVMDisks.Text = 'DISKs'
#$chkVMDisks.Anchor = 'left,top'
for($disk=0;$disk -lt $nrOfDisks;$disk++) {
$chkbDisk = New-Object System.Windows.Forms.Checkbox
$chkbDisk.Location = New-Object System.Drawing.Point(10, ($vShift+($disk*$vShift)) )
#$chkbDisk.Anchor = 'left,top'
$chkbDisk.AutoSize = $true
$chkbDisk.Text = "disk $disk"
$chkbDisk.TabIndex = $allChkb++
$chkVMDisks.Controls.Add($chkbDisk)
#$allChkb++
}
$chkVMBox.Controls.Add($chkVMDisks)
#$lastControl=$chkVMDisks
}
if($nrOfNIC -gt 0) {
#$vLocation = $lastControl.Bottom+$shift
$vLocation = $chkVMDisks.Bottom+$shift
$chkVMNICs = new-object System.Windows.Forms.GroupBox
#$chkVMNICs.MinimumSize = New-Object System.Drawing.Size(180,20)
$chkVMNICs.AutoSize = $true
$chkVMNICs.Location = New-Object System.Drawing.Point(10,$vLocation)
$chkVMNICs.Text = 'NICs'
#$chkVMNICs.Anchor = 'left,top'
for($nic = 0;$nic -lt $nrOfNIC; $nic++) {
$chkbNIC = New-Object System.Windows.Forms.Checkbox
$chkbNIC.Location = New-Object System.Drawing.Point(10, ($vShift+($nic*$vShift)) )
#$chkbNIC.Anchor = 'left,top'
$chkbNIC.AutoSize = $true
$chkbNIC.Text = "nic $nic"
$chkbNIC.TabIndex = $allChkb++
$chkVMNICs.Controls.Add($chkbNIC)
#$allChkb++
}
$chkVMBox.Controls.Add($chkVMNICs)
#$lastControl = $chkVMNICs
}
#$vLocation = $lastControl.Bottom+$shift
$vLocation = $chkVMNICs.Bottom+$shift
$chkbVMdiag = New-Object System.Windows.Forms.Checkbox
$chkbVMdiag.Location = New-Object System.Drawing.Point(10,$vLocation)
#$chkbVMdiag.Anchor = 'left,top'
$chkbVMdiag.AutoSize = $true
$chkbVMdiag.Text = "Boot Diagnostics"
$chkbVMdiag.TabIndex = $allChkb++
$chkVMBox.Controls.Add($chkbVMdiag)
$vLocation = $chkVMBox.bottom + 40
$btOK = New-Object System.Windows.Forms.Button
$btOK.Location = New-Object System.Drawing.Size(15,$vLocation)
$btOK.Size = New-Object System.Drawing.Size(70,20)
$btOK.Text = "OK"
$btOK.DialogResult = [System.Windows.Forms.DialogResult]::OK
#$btOK.Anchor = 'left,bottom'
$btCancel = New-Object System.Windows.Forms.Button
$btCancel.Location = New-Object System.Drawing.Size(95,$vLocation)
$btCancel.Size = New-Object System.Drawing.Size(70,20)
$btCancel.Text = "Cancel"
$btCancel.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
#$btCancel.Anchor = 'right,bottom'
$chkForm.AcceptButton = $btOK
$chkForm.CancelButton = $btCancel
$chkForm.Controls.AddRange(#($chkVMBox, $btOK, $btCancel))
[void]$chkForm.ShowDialog()
effect:
effect of code
there is some big padding on the right of the main form
in check box list - there is additional space in first, double the space in second control
there is some padding under last chkbox
chkVMBox (main box) returns 'size = 100' 'bottom = 110'and buttons which uses this value are actually under other controls and invisible.
seems that ALL VALUES returned with 'autosize' are bullsh*t. so how do you create dynamic layout, not knowing sizes/number of elements upfront?
there is some big padding on the right of the main form
in check box list - there is additional space in first, double the space in second control
there is some padding under last chkbox
Set .AutoSizeMode = 'GrowAndShrink' as well as .MinimumSize properties along with the .AutoSize.
buttons … are actually under other controls and invisible
Button positions are derived from $chkVMBox.bottom; use $chkForm.Controls.Add($chkVMBox) before computing vertical location for buttons and remove $chkVMBox from $chkForm.Controls.AddRange(…)
to create dynamic layout, not knowing sizes/number of elements upfront:
I'd try calculating horizontal and vertical positions and (minimal) sizes of controls relative to an invariant; for instance, those are computed from $chkForm.Font in the following code (aspiration apparently inconsistent:), see the $hShift and $vShift variables, their values and usage.
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
Remove-Variable chk* -ErrorAction SilentlyContinue
$nrOfNIC = 2
$nrOfDisks = 3
$nrOfPIP = 1
$allChkb = 1
$chkForm = New-Object System.Windows.Forms.Form
$chkForm.text = "Remove Resources"
$chkForm.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10)
$hShift = $chkForm.Font.SizeInPoints
$vShift = $chkForm.Font.Height * 1.5 ### arbitrary ad hoc coefficient
$chkForm.StartPosition = 'CenterScreen'
$chkForm.FormBorderStyle = 'Fixed3D'
$chkForm.Icon = [System.Drawing.SystemIcons]::Question
$chkForm.Topmost = $true
$chkForm.AutoSize = $true
$chkForm.AutoSizeMode = 'GrowAndShrink'
$chkForm.MinimumSize = [System.Drawing.Size]::new(100,100)
# $chkForm.AutoScaleMode = 'Font'
$chkForm.MaximizeBox = $false
$chkForm.MinimizeBox = $false
$chkVMBox = new-object System.Windows.Forms.GroupBox
$chkVMBox.AutoSizeMode = 'GrowAndShrink'
$chkVMBox.AutoSize = $true
$chkVMBox.Location = New-Object System.Drawing.Point(10,10)
$chkVMBox.Text = 'VM resources'
if($nrOfDisks -gt 0) {
$chkVMDisks = new-object System.Windows.Forms.GroupBox
$chkVMDisks.Location = New-Object System.Drawing.Point($hShift,( $vShift+10))
$chkVMDisks.Text = 'DISKs'
$chkVMDisks.AutoSize = $true
$chkVMDisks.MinimumSize = [System.Drawing.Size]::new(($chkVMBox.Size.Width - $hShift),$vShift)
$chkVMDisks.AutoSizeMode = 'GrowAndShrink'
for($disk=0;$disk -lt $nrOfDisks;$disk++) {
$chkbDisk = New-Object System.Windows.Forms.Checkbox
$chkbDisk.Location = New-Object System.Drawing.Point($hShift, ($vShift*($disk+1)))
$chkbDisk.AutoSize = $true
$chkbDisk.Text = "disk $disk"
$chkbDisk.TabIndex = $allChkb++
$chkVMDisks.Controls.Add($chkbDisk)
#$allChkb++
}
$chkVMBox.Controls.Add($chkVMDisks)
#$lastControl=$chkVMDisks
}
if($nrOfNIC -gt 0) {
#$vLocation = $lastControl.Bottom+$hShift
$vLocation = $chkVMDisks.Bottom + $vShift
$chkVMNICs = new-object System.Windows.Forms.GroupBox
$chkVMNICs.AutoSize = $true
$chkVMNICs.MinimumSize = [System.Drawing.Size]::new(($chkVMBox.Size.Width - $hShift),$vShift)
$chkVMNICs.AutoSizeMode = 'GrowAndShrink'
$chkVMNICs.Location = New-Object System.Drawing.Point($hShift,$vLocation)
$chkVMNICs.Text = 'NICs'
for($nic = 0;$nic -lt $nrOfNIC; $nic++) {
$chkbNIC = New-Object System.Windows.Forms.Checkbox
$chkbNIC.Location = New-Object System.Drawing.Point($hShift, ($vShift*($nic+1)) )
# $chkbNIC.Anchor = 'left,top'
$chkbNIC.AutoSize = $true
$chkbNIC.Text = "nic $nic"
$chkbNIC.TabIndex = $allChkb++
$chkVMNICs.Controls.Add($chkbNIC)
#$allChkb++
}
$chkVMBox.Controls.Add($chkVMNICs)
#$lastControl = $chkVMNICs
}
#$vLocation = $lastControl.Bottom+$hShift
$vLocation = $chkVMNICs.Bottom + $vShift
$chkbVMdiag = New-Object System.Windows.Forms.Checkbox
$chkbVMdiag.Location = New-Object System.Drawing.Point($hShift,$vLocation)
$chkbVMdiag.AutoSize = $true
$chkbVMdiag.Text = "Boot Diagnostics"
$chkbVMdiag.TabIndex = $allChkb++
$chkVMBox.Controls.Add($chkbVMdiag)
$chkForm.Controls.Add($chkVMBox)
$vLocation = $chkVMBox.bottom + 20
$btOK = New-Object System.Windows.Forms.Button
$btOK.Location = New-Object System.Drawing.Point(15,$vLocation)
# $btOK.Size = New-Object System.Drawing.Size(70,20)
$btOK.AutoSize = $true
$btOK.Text = "OK"
$btOK.DialogResult = [System.Windows.Forms.DialogResult]::OK
$btCancel = New-Object System.Windows.Forms.Button
$btCancel.Location = New-Object System.Drawing.Point(95,$vLocation)
# $btCancel.Size = New-Object System.Drawing.Size(70,20)
$btCancel.AutoSize = $true
$btCancel.Text = "Cancel"
$btCancel.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$chkForm.AcceptButton = $btOK
$chkForm.CancelButton = $btCancel
$chkForm.Controls.AddRange(#($btOK, $btCancel))
[void]$chkForm.ShowDialog()
$chkForm.Dispose()

Powershell Listview : Add 6 items lists in 6 different columns

I have an issue with my PS script including a listview.
I need to check the content of 6 directorys and show the content in 6 different columns :
The issue is that i can't get a correct visual, they are all in the same column
Here is my code :
$Form = New-Object Windows.Forms.Form
$Form.Text = "New Test"
$Form.Width = 1550
$Form.Height = 800
$Listview = New-Object System.Windows.Forms.ListView
$Listview.Location = New-Object System.Drawing.Size(15,10)
$Listview.Size = New-Object System.Drawing.Size(550,10)
$Listview.AutoResizeColumns([System.Windows.Forms.ColumnHeaderAutoResizeStyle]::ColumnContent)
$Listview.View = "Details"
$Listview.FullRowSelect = $true
$Listview.GridLines = $true
$Listview.Height = 650
$Listview.Width =1500
$Listview.AllowColumnReorder = $true
$Listview.Sorting = [System.Windows.Forms.SortOrder]::None
#[void]$Listview.Columns.Add('Null',0)
[void]$Listview.Columns.Add('Scripts',150)
[void]$Listview.Columns.Add('Applications',150)
[void]$Listview.Columns.Add('Systems',500)
[void]$Listview.Columns.Add('Databases',100)
[void]$Listview.Columns.Add('Datas',150)
[void]$Listview.Columns.Add('Backups',500)
$oButton = New-Object Windows.Forms.Button
$oButton.Text = "List"
$oButton.Top = 700
$oButton.Left = 350
$oButton.Width = 150
$oButton.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right
$a = (Get-ChildItem "C:\Scripts\").Name
$b = (Get-ChildItem "C:\Applications\").Name
$c = (Get-ChildItem "C:\Systems\").Name
$d = (Get-ChildItem "C:\Databases\").Name
$e = (Get-ChildItem "C:\Datas\").Name
$f = (Get-ChildItem "C:\Backups\").Name
$Entry = New-Object System.Windows.Forms.ListViewItem($_.Null)
foreach($mp in $a){
$Entry1 = New-Object System.Windows.Forms.ListViewItem($_.Scripts)
$Listview.Items.Add($mp)
}
foreach($mp1 in $b){
$Entry2 = New-Object System.Windows.Forms.ListViewItem($_.Applications)
$Listview.Items.Add($Entry2).SubItems.Add($mp1)
}
foreach($mp2 in $c){
$Entry3 = New-Object System.Windows.Forms.ListViewItem($_.Systems)
$Listview.Items.Add($Entry3).SubItems.Add($mp2)
}
foreach($mp3 in $d){
$Entry4 = New-Object System.Windows.Forms.ListViewItem($_.Databases)
$Listview.Items.Add($Entry4).SubItems.Add($mp3)
}
foreach($mp4 in $e){
$Entry5 = New-Object System.Windows.Forms.ListViewItem($_.Datas)
$Listview.Items.Add($Entry5).SubItems.Add($mp4)
}
foreach($mp5 in $f){
$Entry6 = New-Object System.Windows.Forms.ListViewItem($_.Backup)
$Listview.Items.Add($Entry6).SubItems.Add($mp5)
}
$Form.Add_Shown({$Form.Activate()})
$Form.controls.add($oButton)
$Form.controls.add($Listview)
$Form.ShowDialog()
I tried a lot of change but im still blocked
Thanks in advance for you help
Regards

PowerShell WinForm: Enable button if one or more checkboxes are checked otherwise disable button

Should be pretty straight forward but cant work out the logic. Below is the code I currently have which sort of works but the only problem is if more than one checkboxes are checked and you uncheck one the button disables, I need the button to remain enabled as long as there is one or more checkbox checked. I've also tried various convoluted If and Elseif statements but nothing I've tried changes this behaviour.
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '170,191'
$Form.text = "Scheduler"
$Form.TopMost = $false
$MonCheckBox = New-Object system.Windows.Forms.CheckBox
$MonCheckBox.text = "Monday"
$MonCheckBox.AutoSize = $false
$MonCheckBox.width = 95
$MonCheckBox.height = 20
$MonCheckBox.location = New-Object System.Drawing.Point(30,30)
$MonCheckBox.Font = 'Microsoft Sans Serif,10'
$TueCheckBox = New-Object system.Windows.Forms.CheckBox
$TueCheckBox.text = "Tuesday"
$TueCheckBox.AutoSize = $false
$TueCheckBox.width = 95
$TueCheckBox.height = 20
$TueCheckBox.location = New-Object System.Drawing.Point(30,50)
$TueCheckBox.Font = 'Microsoft Sans Serif,10'
$WedCheckBox = New-Object system.Windows.Forms.CheckBox
$WedCheckBox.text = "Wednesday"
$WedCheckBox.AutoSize = $false
$WedCheckBox.width = 95
$WedCheckBox.height = 20
$WedCheckBox.location = New-Object System.Drawing.Point(30,70)
$WedCheckBox.Font = 'Microsoft Sans Serif,10'
$ThuCheckBox = New-Object system.Windows.Forms.CheckBox
$ThuCheckBox.text = "Thursday"
$ThuCheckBox.AutoSize = $false
$ThuCheckBox.width = 95
$ThuCheckBox.height = 20
$ThuCheckBox.location = New-Object System.Drawing.Point(30,90)
$ThuCheckBox.Font = 'Microsoft Sans Serif,10'
$FriCheckBox = New-Object system.Windows.Forms.CheckBox
$FriCheckBox.text = "Friday"
$FriCheckBox.AutoSize = $false
$FriCheckBox.width = 95
$FriCheckBox.height = 20
$FriCheckBox.location = New-Object System.Drawing.Point(30,110)
$FriCheckBox.Font = 'Microsoft Sans Serif,10'
$SchedButton = New-Object system.Windows.Forms.Button
$SchedButton.text = "Schedule"
$SchedButton.width = 60
$SchedButton.height = 30
$SchedButton.location = New-Object System.Drawing.Point(30,134)
$SchedButton.Font = 'Microsoft Sans Serif,10'
$SchedButton.Autosize = $true
$SchedButton.Enabled = $false
$Form.controls.AddRange(#($MonCheckBox,$TueCheckBox,$WedCheckBox,$ThuCheckBox,$FriCheckBox,$SchedButton))
$MonCheckBox.add_CheckedChanged({$SchedButton.Enabled = $MonCheckBox.Checked})
$TueCheckBox.add_CheckedChanged({$SchedButton.Enabled = $TueCheckBox.Checked})
$WedCheckBox.add_CheckedChanged({$SchedButton.Enabled = $WedCheckBox.Checked})
$ThuCheckBox.add_CheckedChanged({$SchedButton.Enabled = $ThuCheckBox.Checked})
$FriCheckBox.add_CheckedChanged({$SchedButton.Enabled = $FriCheckBox.Checked})
[void]$Form.ShowDialog()
I would do something like this:
$Form.controls.AddRange(#($MonCheckBox, $TueCheckBox, $WedCheckBox, $ThuCheckBox, $FriCheckBox, $SchedButton))
Function Test-AnyButtonChecked {
if (
$MonCheckBox.Checked -or
$TueCheckBox.Checked -or
$WedCheckBox.Checked -or
$ThuCheckBox.Checked -or
$FriCheckBox.Checked
) {
$SchedButton.Enabled = $true
}
else {
$SchedButton.Enabled = $false
}
}
$MonCheckBox.add_CheckedChanged( { Test-AnyButtonChecked })
$TueCheckBox.add_CheckedChanged( { Test-AnyButtonChecked })
$WedCheckBox.add_CheckedChanged( { Test-AnyButtonChecked })
$ThuCheckBox.add_CheckedChanged( { Test-AnyButtonChecked })
$FriCheckBox.add_CheckedChanged( { Test-AnyButtonChecked })
[void]$Form.ShowDialog()

How to make a label of GUI PowerShell has same location in any display executed?

I made a GUI contain some label. Once I execute this code in other computer or notebook, the location of those label changed. How do I make the location can be same no matter the display that I execute the code?
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object System.Windows.Forms.Form
$Form.ClientSize = '578,400'
$Form.text = "Form"
$Form.BackColor = "#c1daf7"
$Form.TopMost = $false
$Form.WindowState = 'Maximized'
$Label1 = New-Object System.Windows.Forms.Label
$Label1.text = "UNDER PROCESS"
$Label1.AutoSize = $true
$Label1.Location = New-Object System.Drawing.Point(600, 300)
$Label1.Font = 'Microsoft Sans Serif,30,style=Bold,Underline'
$Label1.ForeColor = "#d0021b"
$Label2 = New-Object System.Windows.Forms.Label
$Label2.text = "WAITING"
$Label2.AutoSize = $true
$Label2.Location = New-Object System.Drawing.Point(770, 500)
$Label2.Font = 'Microsoft Sans Serif,20,style=Bold'
$Label2.ForeColor = "#fb0505"
$Form.controls.AddRange(#($Label1, $Label2))
[void]$Form.ShowDialog()
UPDATED
I updated my code with a full code.
I tried this but it return me error:
Exception calling "ShowDialog" with "0" argument(s): "Form that is already visible cannot be displayed as a modal dialog box. Set the form's visible property to false before calling showDialog."
Param (
[string]$Path = '*.*',
[string]$MaxAttempts = 5
)
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
# set things up for the timer
$script:nAttempts = 0
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000 # 1 second
$timer.Add_Tick({
$global:Result = $null
$script:nAttempts++
$file = Get-Item -Path $Path
if ($file) {
$global:Result = [PSCustomObject]#{
Exists = $true
FileName = $file.FullName
Attempts = $script:nAttempts
}
$timer.Dispose()
$Form.Close()
}
elseif ($script:nAttempts -ge $MaxAttempts) {
$global:Result = [PSCustomObject]#{
Exists = $false
FileName = ''
Attempts = $script:nAttempts
}
$timer.Dispose()
$Form.Close()
}
})
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '617,418'
$Form.text = "Auto"
$Form.BackColor = "#8b572a"
$Form.TopMost = $false
$Form.WindowState = 'Maximized'
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "UNDER AUTOMATION PROCESS"
$Label1.AutoSize = $true
$Label1.width = 25
$Label1.height = 10
$Label1.Anchor = 'top,right,bottom,left'
$Label1.ForeColor = "#ffffff"
$Label1.Anchor = "None"
$Label1.TextAlign = "MiddleCenter"
$Label2 = New-Object system.Windows.Forms.Label
$Label2.text = "Waiting for the job..."
$Label2.AutoSize = $true
$Label2.width = 25
$Label2.height = 10
$Label2.ForeColor = "#ffffff"
$Label2.Anchor = "None"
$Label2.TextAlign = "MiddleCenter"
$Form.controls.AddRange(#($Label1,$Label2))
[void]$Form.Show()
Write-Host $Form.Height
Write-Host $Form.Width
$Label1.location = New-Object System.Drawing.Point(($Form.Width*0.35), ($Form.Height*0.4))
$Label2.location = New-Object System.Drawing.Point(($form.Width*0.43), ($Form.Height*0.5))
$L_S = (($Form.Width/2) - ($Form.Height / 2)) / 15
$L_S
$Label1.Font = "Microsoft Sans Serif, $L_S, style=Bold"
$Label2.Font = "Microsoft Sans Serif, $L_S, style=Bold"
$Form.controls.AddRange(#($Label1,$Label2))
# start the timer as soon as the dialog is visible
$Form.Add_Shown({ $timer.Start() })
[void]$Form.ShowDialog()
# clean up when done
$Form.Dispose()
I updated my code, I tried this, but it still return me an error. Anyone can help me to fix it please. Thanks
Updated 2nd
Param (
[string]$Path = '*.*',
[string]$MaxAttempts = 5
)
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
# set things up for the timer
$script:nAttempts = 0
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000 # 1 second
$timer.Add_Tick({
$global:Result = $null
$script:nAttempts++
$file = Get-Item -Path $Path
if ($file) {
$global:Result = [PSCustomObject]#{
Exists = $true
FileName = $file.FullName
Attempts = $script:nAttempts
}
$timer.Dispose()
$Form.Close()
}
elseif ($script:nAttempts -ge $MaxAttempts) {
$global:Result = [PSCustomObject]#{
Exists = $false
FileName = ''
Attempts = $script:nAttempts
}
$timer.Dispose()
$Form.Close()
}
})
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
# $Form.ClientSize = '617,418'
$Form.text = "AutoGM"
$Form.BackColor = "#9b9b9b"
$Form.TopMost = $false
$Form.Width = [System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Width
$Form.Height = [System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Height
$FontSize = ($Form.Width / 100) + ($Form.Height/100) + 5
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "UNDER PROCESS"
$Label1.AutoSize = $true
# $Label1.width = 25
# $Label1.height = 10
$Label1.Anchor = "None"
$Label1.Location = New-Object System.Drawing.Point(($form.Width*0.3), ($Form.Height*0.3))
$Label1.Font = "Microsoft Sans Serif,$FontSize,style=Bold"
$Label1.ForeColor = "#000000"
$Label2 = New-Object system.Windows.Forms.Label
$Label2.text = "Waiting..."
$Label2.AutoSize = $true
# $Label2.width = 25
# $Label2.height = 10
$Label2.Location = New-Object System.Drawing.Point(($form.Width*0.4), ($Form.Height*0.4))
$Label2.Anchor = "None"
$Label2.Font = "Microsoft Sans Serif,$FontSize"
$Label2.ForeColor = "#000000"
$img = [System.Drawing.Image]::Fromfile(".\img.png")
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Location = New-Object System.Drawing.Point(($form.Width*0.45), ($Form.Height*0.5))
$pictureBox.Width = $Form.Size.Width / 5
$pictureBox.Height = $Form.Size.Height / 5
$pictureBox.Image = $img
$form.controls.add($pictureBox)
$Form.controls.AddRange(#($Label1,$Label2))
# Write-Host $Form.Height
# Write-Host $Form.Width
# $Label1.location = New-Object System.Drawing.Point(($Form.Width*0.35), ($Form.Height*0.4))
# $Label2.location = New-Object System.Drawing.Point(($form.Width*0.43), ($Form.Height*0.5))
# $L_S = (($Form.Width/2) - ($Form.Height / 2)) / 15
# $Label1.Font = "Microsoft Sans Serif, $L_S, style=Bold"
# $Label2.Font = "Microsoft Sans Serif, $L_S, style=Bold"
# $Form.controls.AddRange(#($Label1,$Label2))
# start the timer as soon as the dialog is visible
$Form.Add_Shown({ $timer.Start() })
[void]$Form.ShowDialog()
# clean up when done
$Form.Dispose()
As stated by Niraj it depends on the resolution of the monitor being used so instead ask yourself where do you want it to show? in mid-mid? mid-left? this can be achieved using some math, see below for a simple example, it uses the $form width en height to calculate the correct position for the labels.
$Label1.Location = New-Object System.Drawing.Point(($form.Width*0.5), ($Form.Height*0.5))
$Label2.Location = New-Object System.Drawing.Point(($form.Width*0.5), ($Form.Height*0.4))

return value from function with combobox

I have a small GUI that will connect to a vCenter in a list. The forms open in the order I want, but I can't carry values between them. I've tried so many return calls I don't know what else to do.
Here is the code. Basically, the first form opens and asks for which vCenter you want. Then prompts you to authenticate (this all works wonderfully). But the problem comes in after the script is done. I don't get any return values.
function Connect-Vc ($vcArg)
{
$Form2 = $null
$selectedVC = $vcArg.selecteditem
$viconnection = $null
if ($global:defaultviserver -ne $null)
{
Disconnect-viserver * -confirm:$false
}
$clusterselection = $null
$DropDownTo = $null
$credential = Get-Credential -Message "Please Enter Elevated Credentials To Access $selectedVC. You can enter assuming you're an administrator on $selectedVC."
$username = $credential.Username
$password = $credential.GetNetworkCredential().Password
$viconnection = connect-viserver $selectedVC -User $username -Password $password
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Windows.Forms.Application]::EnableVisualStyles()
$Form2 = New-Object System.Windows.Forms.Form
$Form2.AutoSize = $True
$Form2.AutoSizeMode = "GrowAndShrink"
$Form2.width = 600
$Form2.height = 500
$Form2.Text = ”Vcenter Selection”
$Form2.StartPosition = "CenterScreen"
#First item selection*******************
$DropDownLabel1 = new-object System.Windows.Forms.Label
$DropDownLabel1.Location = new-object System.Drawing.Size(140,10)
$DropDownLabel1.size = new-object System.Drawing.Size(130,30)
$DropDownLabel1.Text = "$selectedVC"
$Form2.Controls.Add($DropDownLabel1)
$DropDownLabel2 = new-object System.Windows.Forms.Label
$DropDownLabel2.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel2.size = new-object System.Drawing.Size(100,40)
$DropDownLabel2.Text = "Connected to Vcenter"
$Form2.Controls.Add($DropDownLabel2)
#Second item selection*******************
[array]$clusterselection = get-cluster
$DropDownTo = New-Object System.Windows.Forms.ComboBox
$DropDownTo.Location = New-Object System.Drawing.Size(140,50)
$DropDownTo.Size = New-Object System.Drawing.Size(130,30)
$DropdownTo.DropDownStyle = 2
ForEach ($Item2 in $clusterselection)
{
[void] $DropDownTo.Items.Add($Item2)
}
$Form2.Controls.Add($DropDownTo)
$DropDownLabel3 = new-object System.Windows.Forms.Label
$DropDownLabel3.Location = new-object System.Drawing.Size(10,50)
$DropDownLabel3.size = new-object System.Drawing.Size(100,40)
$DropDownLabel3.Text = "Select Cluster Location"
$Form2.Controls.Add($DropDownLabel3)
#Third item selection*******************
$DropDownType = New-Object System.Windows.Forms.ComboBox
$DropDownType.Location = New-Object System.Drawing.Size(140,90)
$DropDownType.Size = New-Object System.Drawing.Size(130,30)
$DropDownType.DropDownStyle = 2
ForEach ($Item3 in $typeOfServer)
{
[void] $DropDownType.Items.Add($Item3)
}
$Form2.Controls.Add($DropDownType)
$DropDownLabel4 = new-object System.Windows.Forms.Label
$DropDownLabel4.Location = new-object System.Drawing.Size(10,90)
$DropDownLabel4.size = new-object System.Drawing.Size(100,40)
$DropDownLabel4.Text = "Select Type of Server"
$Form2.Controls.Add($DropDownLabel4)
#Fourth item Input*******************
$InputName = New-Object System.Windows.Forms.Textbox
$InputName.Location = New-Object System.Drawing.Size(140,130)
$InputName.Size = New-Object System.Drawing.Size(130,30)
$InputName.MaxLength = 15
$Form2.Controls.Add($InputName)
$inputLabel = new-object System.Windows.Forms.Label
$inputLabel.Location = new-object System.Drawing.Size(10,130)
$inputLabel.size = new-object System.Drawing.Size(100,40)
$inputLabel.Text = "Enter Name of Server"
$Form2.Controls.Add($inputLabel)
#Fifth item selection*******************
$dropDownMem = New-Object System.Windows.Forms.Combobox
$dropDownMem.Location = New-Object System.Drawing.Size(140,170)
$dropDownMem.Size = New-Object System.Drawing.Size(130,30)
$dropDownMem.DropDownStyle = 2
ForEach ($Item5 in $mem)
{
[void] $dropDownMem.Items.Add($Item5)
}
$Form2.Controls.Add($dropDownMem)
$DropDownLabel5 = new-object System.Windows.Forms.Label
$DropDownLabel5.Location = new-object System.Drawing.Size(10,170)
$DropDownLabel5.size = new-object System.Drawing.Size(100,40)
$DropDownLabel5.Text = "Select Memory Quantity in GB"
$Form2.Controls.Add($DropDownLabel5)
#Sixth item selection*******************
$dropDownvCpu = New-Object System.Windows.Forms.Combobox
$dropDownvCpu.Location = New-Object System.Drawing.Size(140,210)
$dropDownvCpu.Size = New-Object System.Drawing.Size(130,30)
$dropDownvCpu.DropDownStyle = 2
ForEach ($Item6 in $vCpu)
{
[void] $dropDownvCpu.Items.Add($Item6)
}
$Form2.Controls.Add($dropDownvCpu)
$DropDownLabel6 = new-object System.Windows.Forms.Label
$DropDownLabel6.Location = new-object System.Drawing.Size(10,210)
$DropDownLabel6.size = new-object System.Drawing.Size(100,40)
$DropDownLabel6.Text = "Select Virtual CPU's"
$Form2.Controls.Add($DropDownLabel6)
#Seventh item selection*******************
$dropDownOS = New-Object System.Windows.Forms.Combobox
$dropDownOS.Location = New-Object System.Drawing.Size(140,250)
$dropDownOS.Size = New-Object System.Drawing.Size(130,30)
$dropDownOS.DropDownStyle = 2
ForEach ($Item7 in $operatingSystem)
{
[void] $dropDownOS.Items.Add($Item7)
}
$Form2.Controls.Add($dropDownOS)
$DropDownLabel7 = new-object System.Windows.Forms.Label
$DropDownLabel7.Location = new-object System.Drawing.Size(10,250)
$DropDownLabel7.size = new-object System.Drawing.Size(100,40)
$DropDownLabel7.Text = "Select Operating System"
$Form2.Controls.Add($DropDownLabel7)
#Defines buttons for click to accept*********************
#$Button1 = new-object System.Windows.Forms.Button
#$Button1.Location = new-object System.Drawing.Size(10,300)
#$Button1.Size = new-object System.Drawing.Size(100,20)
#$Button1.Text = "Provision Vm"
#$Button1.Add_Click({Provision-VM})
#$Button1.Add_Click({Get-Account $oRphanedAcct})
#Defines buttons for click to accept*********************
$Button2 = new-object System.Windows.Forms.Button
$Button2.Location = new-object System.Drawing.Size(10,330)
$Button2.Size = new-object System.Drawing.Size(100,20)
$Button2.Text = "Provision Vm"
$Button2.Add_Click({Provision-VM -arg1 "$choice4" -arg2 "$choice5" -arg3 "$Choice3"})
#Defines buttons for click to accept*********************
$Button3 = new-object System.Windows.Forms.Button
$Button3.Location = new-object System.Drawing.Size(140,330)
$Button3.Size = new-object System.Drawing.Size(100,20)
$Button3.Text = "Close Form"
#$Button3.Add_Click({Return-Drop})
$Button3.Add_Click({$Form2.Dispose()})
#Defines buttons for click to accept*********************
$Button4 = new-object System.Windows.Forms.Button
$Button4.Location = new-object System.Drawing.Size(275,330)
$Button4.Size = new-object System.Drawing.Size(100,20)
$Button4.Text = "Ok Button"
$Button4.Dialogresult = [System.Windows.Forms.DialogResult]::OK
$Form2.AcceptButton = $Button4
$Form2.Controls.Add($Button2)
$Form2.Controls.Add($Button3)
$Form2.Controls.Add($Button4)
$Form2.Topmost = $True
$Form2.ShowDialog()
$Form2.refresh()
return $DropdownTo.SelectedItem
return $DropDownType.SelectedItem.ToString()
return $InputName.SelectedItem.ToString()
return $DropDownMem.SelectedItem.ToString()
return $DropDownVcpu.SelectedItem.ToString()
return $DropDownOS.SelectedItem.ToString()
return $Button3
return $Button4
}
#Main function definition********************
function Menu
{
$clusterselection = $null
$global:defaultviserver = $null
[array]$VCItems = "VCSelection","VCSelection2","VCSelection3"
[array]$typeOfServer = "Vanilla", "IIS", "SQL", "SQL_Cluster_Node"
[array]$operatingSystem = "Windows2012", "Windows2016"
[array]$mem = "2","4","6","8","10","12","32","64"
[array]$vCpu = "2","4","6","8","10","12"
#Function for Closing Form Upon no input
function Return-Drop
{
If (($dropDownVC.SelectedItem -ne $null) -or ($DropDownTO.SelectedItem -ne $null) -or ($DropDownType.SelectedItem -ne $null))
{
$Form.Close()
}
}
#Function for connecting to Vcenter
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object System.Windows.Forms.Form
$Form.AutoSize = $True
$Form.AutoSizeMode = "GrowAndShrink"
$Form.width = 600
$Form.height = 500
$Form.Text = ”Vcenter Selection”
$Form.StartPosition = "CenterScreen"
#First item selection*******************
$DropDownVC = New-Object System.Windows.Forms.ComboBox
$DropDownVC.Location = New-Object System.Drawing.Size(140,10)
$DropDownVC.Size = New-Object System.Drawing.Size(130,30)
$DropDownVC.DropDownStyle = 2
$DropDownVC.TabIndex = 0
ForEach ($Item1 in $VCItems)
{
[void] $DropDownVC.Items.Add($Item1)
}
$DropDownVC.SelectedItem = $DropDownVC.Items[0]
$Form.Controls.Add($DropDownVC)
$DropDownLabel1 = new-object System.Windows.Forms.Label
$DropDownLabel1.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel1.size = new-object System.Drawing.Size(100,40)
$DropDownLabel1.Text = "Select Vcenter to connect to"
$Form.Controls.Add($DropDownLabel1)
#Defines buttons for click to connect to VC*********************
$Button5 = new-object System.Windows.Forms.Button
$Button5.Location = new-object System.Drawing.Size(290,10)
$Button5.Size = new-object System.Drawing.Size(100,20)
$Button5.Text = "Connect"
$Button5.Add_Click({$form.visible = $false;$t = Connect-Vc -vcArg $DropDownVC})
$Form.Controls.Add($Button5)
$Form.Topmost = $True
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()
$Choice1 = $DropDownVC.SelectedItem.ToString()
enter code here
return $Choice1
}

Resources