2016-11-21 152 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