I’ve searched Google and this site thoroughly and haven’t been able to find the right solution to this. Any help will be appreciated.
I have trimmed this down to only the problem sections of my full script for posting. This script produces a form that displays a dynamically created list of radio buttons inside a group box. The script uses an external file to load the item list but for this I just added a hard list.
My goal is that each time any radio button is selected a function runs that populates another part of the form (not included) with data. Until OK is clicked the radio buttons can be randomly selected which changes the data the function loads. I’ve tried numerous examples of scanning for changes but cant get any to work. If I add explicit click events things work fine but I want it dynamic since the number of items in the item list determines the final size of the form.
Some examples I’ve tried are commented out at the bottom of the script.
As listed below the form works with explicit event handlers and prints the selected radio button text to the screen.
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$ScreenSize = (Get-CimInstance -Class Win32_DesktopMonitor | Select-Object ScreenWidth,ScreenHeight)
If ($ScreenSize.Count -gt 1){ #--[ Detect multiple monitors ]--
# More than 1 monitor detected...
ForEach ($Resolution in $ScreenSize){
If ($Null -ne $Resolution.ScreenWidth){
$ScreenWidth = $Resolution.ScreenWidth
$ScreenHeight = $Resolution.ScreenHeight
Break
}
}
}Else{
$ScreenWidth = $Resolution.ScreenWidth
$ScreenHeight = $Resolution.ScreenHeight
}
$SiteList = @()
$SiteList += "site 1"
$SiteList += "site 2"
$SiteList += "site 3"
$SiteList += "site 4"
$SiteList += "site 5"
$SiteList += "site 6"
$SiteList += "site 7"
$SiteList += "site 8"
Write-host $SiteList.Count
#--[ Define Form ]--------------------------------------------------------------
[int]$FormWidth = 750
If ($SiteList.Count/2 -is [int]){
[int]$FormHeight = ((($SiteList.Count/2)*20)+255) #--[ Dynamically Created Variable for Box Size (Even count) ]--
}Else{
[int]$FormHeight = ((($SiteList.Count/2)*23)+255) #--[ Dynamically Created Variable for Box Size (Odd count) ]--
}
[int]$FormHCenter = ($FormWidth / 2) # 170 Horizontal center point
[int]$FormVCenter = ($FormHeight / 2) # 209 Vertical center point
[int]$ButtonHeight = 25
[int]$TextHeight = 20
$Form = New-Object System.Windows.Forms.Form
$Form.minimumSize = New-Object System.Drawing.Size($FormWidth,$FormHeight)
$Form.maximumSize = New-Object System.Drawing.Size($FormWidth,($FormHeight+80))
$CbLeft = 17
$CbRight = 163
$CbHeight = 30
$CbVar = 20
$CbBox = 145
$Count = 0
$Range = @()
$GroupBox = New-Object System.Windows.Forms.GroupBox
While ($SiteList.Count -gt $Count) {
#--[ Left Checkbox ]--
Remove-Variable -Name "RadioButton$Count" -ErrorAction SilentlyContinue
$Left = new-object System.Windows.Forms.radiobutton -Property @{
Location = new-object System.Drawing.Size($CbLeft,$CbHeight)
Size = new-object System.Drawing.Size($CbBox,$TextHeight)
Text = $SiteList[$Count]
Enabled = $true
}
New-Variable -Name "RadioButton$Count" -value $Left
$LeftBox = Get-Variable -name "RadioButton$Count" -ValueOnly
$Range += $LeftBox
$Count++
#--[ Right Checkbox ]--
Remove-Variable -Name "RadioButton$Count" -ErrorAction SilentlyContinue
$Right = New-Object System.Windows.Forms.radiobutton -Property @{
Location = new-object System.Drawing.Size($CbRight,$CbHeight)
Size = new-object System.Drawing.Size($CbBox,$TextHeight)
Text = $SiteList[$Count]
Enabled = $true
}
New-Variable -Name "RadioButton$Count" -value $Right
$RightBox = Get-Variable -name "RadioButton$Count" -ValueOnly
$Range += $RightBox
$Count++
$CbHeight = $CbHeight+$CbVar
}
write-host "site count " $Count
$GroupBox.Location = '35,95'
$GroupBox.size = '310,135'
$GroupBox.text = "Locations"
$GroupBox.Controls.AddRange($Range)
$form.controls.add($GroupBox)
$BoxLength = 100
$LineLoc = 30
$InfoBox = New-Object System.Windows.Forms.TextBox
$InfoBox.Location = New-Object System.Drawing.Size((($FormHCenter-($BoxLength/2))-10),$LineLoc)
$InfoBox.Size = New-Object System.Drawing.Size($BoxLength,$TextHeight)
$InfoBox.Enabled = $False
$InfoBox.TextAlign = 2
$Form.Controls.Add($InfoBox) #>
$BoxLength = 100
$LineLoc = $FormHeight-77
$CloseButton = new-object System.Windows.Forms.Button
$CloseButton.Location = New-Object System.Drawing.Size(($FormHCenter-($BoxLength/2)-75),$LineLoc)
$CloseButton.Size = new-object System.Drawing.Size($BoxLength,$ButtonHeight)
$CloseButton.TabIndex = 1
$CloseButton.Text = "Cancel/Close"
$CloseButton.Add_Click({
$Form.Close()
$Form.Dispose()
})
$Form.Controls.Add($CloseButton)
$ProcessButton = new-object System.Windows.Forms.Button
$ProcessButton.Location = new-object System.Drawing.Size(($FormHCenter-($BoxLength/2)+55),$LineLoc)
$ProcessButton.Size = new-object System.Drawing.Size($BoxLength,$ButtonHeight)
$ProcessButton.Enabled = $false #true
$ProcessButton.Text = "Execute"
$Form.Controls.Add($ProcessButton)
$event1={
if ($RadioButton0.Checked){$InfoBox.Text = "You selected 1"}
if ($RadioButton1.Checked){$InfoBox.Text = "You Selected 2"}
if ($RadioButton2.Checked){$InfoBox.Text = "You selected 3"}
if ($RadioButton3.Checked){$InfoBox.Text = "You Selected 4"}
if ($RadioButton4.Checked){$InfoBox.Text = "You Selected 5"}
if ($RadioButton5.Checked){$InfoBox.Text = "You Selected 6"}
if ($RadioButton6.Checked){$InfoBox.Text = "You Selected 7"}
if ($RadioButton7.Checked){$InfoBox.Text = "You Selected 8"}
}
$RadioButton0.Add_Click($event1)
$RadioButton1.Add_Click($event1)
$RadioButton2.Add_Click($event1)
$RadioButton3.Add_Click($event1)
$RadioButton4.Add_Click($event1)
$RadioButton5.Add_Click($event1)
$RadioButton6.Add_Click($event1)
$RadioButton7.Add_Click($event1)
#>
# Where-Object {$PSItem -is [system.windows.controls.radiobutton] -and $PSItem.IsChecked} | Select-Object Name
<#
$Form.Controls | Where-Object { $Item -is [System.Windows.Forms.RadioButton] } | ForEach-Object {
$Item.Add_Click({
If (-Not $ProcessButton.Enabled){
$ProcessButton.Enabled = $True
}
})
}#>
$Form.topmost = $true
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
Kcmjr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.