单击单选按钮时调用脚本块

Invoking a scriptblock when clicking a radio button

我希望我的 GUI 能够根据用户单击单选按钮 1 或单选按钮 2 动态生成事件。

我在这方面看到了一些不错的 tutorials 但它是在单击按钮时调用的。我想将其称为 单选按钮 的 OnClick。

这是我目前的代码:

#Load Assemblies
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null

[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") | Out-Null

$net = New-Object -ComObject Wscript.Network

Add-Type -AssemblyName System.Windows.Forms


#Create the Form

#Draw form
function SQLDoTasks{

$Form = New-Object System.Windows.Forms.Form

$Form.width = 800

$Form.height = 600

$Form.BackColor = "lightblue"

$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D

$Form.Text = "Daily DBA Tasks"

$Form.maximumsize = New-Object System.Drawing.Size(525,350)

$Form.startposition = "centerscreen"

$Form.KeyPreview = $True

$Form.Add_KeyDown({if ($_.KeyCode -eq "Enter") {}})

$Form.Add_KeyDown({if ($_.KeyCode -eq "Escape")

     {$Form.Close()}})

# Create a group that will contain your radio buttons
$MyGroupBox = New-Object System.Windows.Forms.GroupBox
$MyGroupBox.Location = '40,30'
$MyGroupBox.size = '400,150'
$MyGroupBox.text = "Specify the Sql environment?"

#Declare option buttons to allow user to select Dev or System Test
$RadioButton1 = New-Object System.Windows.Forms.RadioButton #create the radio button
    $RadioButton1.Location = '20,40'
    $RadioButton1.size = '350,20'
$RadioButton1.Checked = $false #is checked by default
$RadioButton1.Text = "Dev" #labeling the radio button
$RadioButton1.Checked($event)

#Declare option buttons to allow user to select Dev OR System Test
$RadioButton2 = New-Object System.Windows.Forms.RadioButton #create the radio button
$RadioButton2.Location = '20,70'
$RadioButton2.size = '350,20'
#$RadioButton2.Location = new-object System.Drawing.Point(10,40) #location of the radio button(px) in relation to the group box's edges (length, height)
#$RadioButton2.size = New-Object System.Drawing.Size(80,20) #the size in px of the radio button (length, height)
$RadioButton2.Checked = $false #is checked by default
$RadioButton2.Text = "System test" #labeling the radio button
$RadioButton2.Checked($event)

$event={
        if ($RadioButton1.Checked){
               [System.Windows.Forms.MessageBox]::Show("You select Dev")}
        elseif ($RadioButton2.Checked){
               [System.Windows.Forms.MessageBox]::Show("You Selected System Test")}


}
#Create List Box

$ListBox1 = New-Object System.Windows.Forms.ListBox

$ListBox1.Location = New-Object System.Drawing.Size(20,200)

$ListBox1.Size = New-Object System.Drawing.Size(260,20)

$ListBox1.Height = 80

#POPULATE WHAT IT WILL HOLD 

$Servers = get-content -path "xxxx.csv"
write-host $Servers
ForEach ($Server in $Servers){

                #$NL = "`r`n"

                [void] $ListBox1.Items.Add($Server)

                }


#Create the Form
# Add all the GroupBox controls on one line

$Form.Controls.Add($ListBox1)
$form.Controls.AddRange(@($MyGroupBox))
$MyGroupBox.Controls.AddRange(@($Radiobutton1,$RadioButton2))

$Form.Add_Shown({$Form.Activate()})
$dialogResult =$Form.ShowDialog()

}
SQLDoTasks

首先,您必须在添加事件处理程序之前声明 $event 脚本块。所以我会定义两个按钮,然后是脚本块,然后 然后 添加处理程序。此外,您可能应该使用 Add_Click 回调:

#....    
$RadioButton2 = New-Object System.Windows.Forms.RadioButton #create the radio button
    $RadioButton2.Location = '20,70'
    $RadioButton2.size = '350,20'
    #$RadioButton2.Location = new-object System.Drawing.Point(10,40) #location of the radio button(px) in relation to the group box's edges (length, height)
    #$RadioButton2.size = New-Object System.Drawing.Size(80,20) #the size in px of the radio button (length, height)
    $RadioButton2.Checked = $false #is checked by default
    $RadioButton2.Text = "System test" #labeling the radio button

    $event={
             if ($RadioButton1.Checked){
                   [System.Windows.Forms.MessageBox]::Show("You select Dev")}
            elseif ($RadioButton2.Checked){
                   [System.Windows.Forms.MessageBox]::Show("You Selected System Test")}
    }


$RadioButton1.Add_Click($event)
$RadioButton2.Add_Click($event)