2016-11-21 117 views
2

我想我的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 
+0

非常感谢您对我已经使用现有的资源(见下文)#http://serverfixes.com/powershell-forms-part4-radio-buttons-分组 #https://sysadminemporium.wordpress.com/2012/12/07/powershell-gui-for-your-scripts-episode-3/ #http://www.windowsnetworking.com/articles-tutorials/netgeneral /building-powershell-gui-part9.html – ADTJOB

回答

1

首先,你有你添加事件处理程序之前申报$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) 
+0

非常感谢。很高兴知道我在那里。我确实使用过Add_Click,但显然我错了我放置$ event的地方。 – ADTJOB