Dynamically detecting selected radio button in PowerShell form

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()

New contributor

Kcmjr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật