在不使用其他按钮的情况下激活单选按钮

时间:2019-03-21 18:31:44

标签: powershell radio-button

这可能是一个愚蠢的问题,但是我似乎无法正常工作,而且我一直在进行的研究没有帮助。如果我添加一个按钮并在其中添加一个处理程序,则可以使它工作,但是我想看看是否有可能在激活收音机后自动激活它。每当单击其中一个单选按钮时,它将激活而无需其他功能。

这是我的完整代码,以显示我一直在尝试做的事情。



$hddenfile = (Get-ItemPropertyValue 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced' 'Hidden')
$hidextensions = (Get-ItemPropertyValue 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced' 'HideFileExt') 


[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null

$Form = New-Object System.Windows.Forms.Form
$Form.Text = "File Options"
$Form.Name = "form1"
$Form.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 200
$System_Drawing_Size.Height = 275
$Form.ClientSize = $System_Drawing_Size
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 27
$System_Drawing_Point.Y = 190
$Form.Location = $System_Drawing_Point
$Form.WindowState = "Normal"
$Form.Opacity = 1.0
$Form.StartPosition = "CenterScreen"


$Label1                          = New-Object system.Windows.Forms.Label
$Label1.text                     = "Hidden Files and Folders"
$Label1.AutoSize                 = $true
$Label1.width                    = 25
$Label1.height                   = 10
$Label1.location                 = New-Object System.Drawing.Point(23,25)
$Label1.Font                     = 'Microsoft Sans Serif,10'

$Groupbox1                       = New-Object system.Windows.Forms.Groupbox
$Groupbox1.height                = 70
$Groupbox1.width                 = 116
$Groupbox1.location              = New-Object System.Drawing.Point(18,54)

$RadioButton1                    = New-Object system.Windows.Forms.RadioButton
$RadioButton1.Name               = 'Radio1'
$RadioButton1.text               = "Hide Files"
$RadioButton1.AutoSize           = $true
$RadioButton1.width              = 104
$RadioButton1.height             = 20
$RadioButton1.location           = New-Object System.Drawing.Point(17,14)
$RadioButton1.Font               = 'Microsoft Sans Serif,10'
$RadioButton1.Checked            = if($hddenfile -eq 2){ $true } else{$false}       
$RadioButton1.add_Click($handler_RadioButton1_Click)

$RadioButton2                    = New-Object system.Windows.Forms.RadioButton
$RadioButton2.Name               = 'Radio2'
$RadioButton2.text               = "Show Files"
$RadioButton2.AutoSize           = $true
$RadioButton2.width              = 104
$RadioButton2.height             = 20
$RadioButton2.location           = New-Object System.Drawing.Point(17,46)
$RadioButton2.Font               = 'Microsoft Sans Serif,10'
$RadioButton2.Checked            = if($hddenfile -eq 1){ $true } else{$false}   
$RadioButton2.add_Click($handler_RadioButton2_Click)

$Groupbox2                       = New-Object system.Windows.Forms.Groupbox
$Groupbox2.height                = 78
$Groupbox2.width                 = 150
$Groupbox2.location              = New-Object System.Drawing.Point(18,161)

$RadioButton3                    = New-Object system.Windows.Forms.RadioButton
$RadioButton3.text               = "Hide Extensions"
$RadioButton3.AutoSize           = $true
$RadioButton3.width              = 104
$RadioButton3.height             = 20
$RadioButton3.location           = New-Object System.Drawing.Point(20,19)
$RadioButton3.Font               = 'Microsoft Sans Serif,10'
$RadioButton3.Checked            = if($hidextensions -eq 2){ $true } else{$false}       

$RadioButton4                    = New-Object system.Windows.Forms.RadioButton
$RadioButton4.text               = "Show Extensions"
$RadioButton4.AutoSize           = $true
$RadioButton4.width              = 104
$RadioButton4.height             = 20
$RadioButton4.location           = New-Object System.Drawing.Point(20,49)
$RadioButton4.Font               = 'Microsoft Sans Serif,10'
$RadioButton4.Checked            = if($hidextensions -eq 1){ $true } else{$false}       

$Label2                          = New-Object system.Windows.Forms.Label
$Label2.text                     = "Extensions for Known Files"
$Label2.AutoSize                 = $true
$Label2.width                    = 25
$Label2.height                   = 10
$Label2.location                 = New-Object System.Drawing.Point(23,137)
$Label2.Font                     = 'Microsoft Sans Serif,10'

$Form.Controls.Add($Label1)
$Form.Controls.Add($Label2)
$Form.Controls.Add($Groupbox1)
$Form.Controls.Add($Groupbox2)
$Form.Controls.Add($RadioButton1)
$Form.Controls.Add($RadioButton2)
$Form.Controls.Add($RadioButton3)
$Form.Controls.Add($RadioButton4)

$RadioButton1.Check


$Form.controls.AddRange(@($Label1,$Groupbox1,$Groupbox2,$Label2))
$Groupbox1.controls.AddRange(@($RadioButton1,$RadioButton2))
$Groupbox2.controls.AddRange(@($RadioButton3,$RadioButton4))

$RadioButton1_CheckedChanged={
             if($RadioButton1.Checked -eq $true){
                   (Set-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced -Name Hidden -Value 2)}}

$RadioButton2_CheckedChanged={
             if($RadioButton2.Checked -eq $true){
                   (Set-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced -Name Hidden -Value 1)}}

$Form.add_Load($OnLoadForm_StateCorrection)
$Form.ShowDialog()| Out-Null



Pause


任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:0)

在问了问题大约10分钟后就弄明白了。

这是我用于Button1的代码示例:

$RadioButton1.add_Click({Set-ItemProperty -Path HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced -Name Hidden -Value 2})