JOhn
JOhn

Reputation: 101

PowerShell check which dynamically created Radio Button is checked

Here is my code. I am having trouble finding a way to determine what Radio Button has been selected after creating them like this.

$locationY = [int]10
foreach($type in $labels){
    # Create the collection of radio buttons
    $RadioButton = New-Object System.Windows.Forms.RadioButton
    $RadioButton.Location = "20,$(30+$locationY)"
    $RadioButton.size = '350,20'
    if($type -eq 'Chrysler'){$RadioButton.Checked = $true}else{$RadioButton.Checked = $false}
    $RadioButton.Text = $type  
    $RadioButton.Name = $type 
    $Form.Controls.Add($RadioButton)
}

Upvotes: 0

Views: 3566

Answers (1)

Prasoon Karunan V
Prasoon Karunan V

Reputation: 3043

You can use a GroupBox here and see which one is checked form the group box like below.

# code to define groupbox control

$locationY = [int]10
foreach($type in $labels){
    # Create the collection of radio buttons
    $RadioButton = New-Object System.Windows.Forms.RadioButton
    $RadioButton.Location = "20,$(30+$locationY)"
    $RadioButton.size = '350,20'
    if($type -eq 'Chrysler'){$RadioButton.Checked = $true}else{$RadioButton.Checked = $false}
    $RadioButton.Text = $type  
    $RadioButton.Name = $type 
    $Form.Controls.Add($RadioButton)
    $GroupBox.Controls.Add($RadioButton)
}

$ClickedRadioButton = $GroupBox.Controls | Where-Object -FilterScript {$_.Checked}

Upvotes: 1

Related Questions