动态检测PowerShell表单中选定的单选按钮

问题描述 投票:0回答:1

我彻底搜索了谷歌和这个网站,但未能找到正确的解决方案。 任何帮助将不胜感激。

我已将其精简为仅发布完整脚本的问题部分。 该脚本生成一个表单,在组框中显示动态创建的单选按钮列表。 该脚本使用外部文件来加载项目列表,但为此我只是添加了一个硬列表。

我的目标是,每次选择任何单选按钮时,都会运行一个函数,用数据填充表单的另一部分(未包含)。 在单击“确定”之前,可以随机选择单选按钮,这会更改函数加载的数据。 我已经尝试了许多扫描更改的示例,但无法使任何工作正常工作。 如果我添加显式单击事件,则一切正常,但我希望它是动态的,因为项目列表中的项目数量决定了表单的最终大小。

我尝试过的一些示例已在脚本底部注释掉。

如下所列,该表单与显式事件处理程序一起使用,并将选定的单选按钮文本打印到屏幕上。

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
$ScreenSize = (Get-CimInstance -Class Win32_DesktopMonitor | Select-Object ScreenWidth,ScreenHeight)

If ($ScreenSize.Count -gt 1){  #--[ Detect multiple monitors ]--
    # More than 1 monitor detected...
    ForEach ($Resolution in $ScreenSize){
        If ($Null -ne $Resolution.ScreenWidth){
            $ScreenWidth = $Resolution.ScreenWidth
            $ScreenHeight = $Resolution.ScreenHeight
            Break
        }
    }
}Else{
    $ScreenWidth = $Resolution.ScreenWidth
    $ScreenHeight = $Resolution.ScreenHeight
}

$SiteList = @()
$SiteList += "site 1"
$SiteList += "site 2"
$SiteList += "site 3"
$SiteList += "site 4" 
$SiteList += "site 5"
$SiteList += "site 6"
$SiteList += "site 7"
$SiteList += "site 8"

Write-host $SiteList.Count

#--[ Define Form ]--------------------------------------------------------------
[int]$FormWidth = 750
If ($SiteList.Count/2 -is [int]){
    [int]$FormHeight = ((($SiteList.Count/2)*20)+255)   #--[ Dynamically Created Variable for Box Size (Even count) ]--
}Else{
    [int]$FormHeight = ((($SiteList.Count/2)*23)+255)   #--[ Dynamically Created Variable for Box Size (Odd count) ]--
}

[int]$FormHCenter = ($FormWidth / 2)   # 170 Horizontal center point
[int]$FormVCenter = ($FormHeight / 2)  # 209 Vertical center point
[int]$ButtonHeight = 25
[int]$TextHeight = 20

$Form = New-Object System.Windows.Forms.Form    
$Form.minimumSize = New-Object System.Drawing.Size($FormWidth,$FormHeight)
$Form.maximumSize = New-Object System.Drawing.Size($FormWidth,($FormHeight+80))

$CbLeft = 17
$CbRight = 163
$CbHeight = 30
$CbVar = 20
$CbBox = 145

$Count = 0


$Range = @()
$GroupBox = New-Object System.Windows.Forms.GroupBox
While ($SiteList.Count -gt $Count) {
    #--[ Left Checkbox ]--
    Remove-Variable -Name "RadioButton$Count" -ErrorAction SilentlyContinue
    $Left = new-object System.Windows.Forms.radiobutton -Property @{
        Location = new-object System.Drawing.Size($CbLeft,$CbHeight)
        Size = new-object System.Drawing.Size($CbBox,$TextHeight)
        Text = $SiteList[$Count]
        Enabled = $true 
    }
    
    New-Variable -Name "RadioButton$Count" -value $Left
    $LeftBox = Get-Variable -name "RadioButton$Count" -ValueOnly
    $Range += $LeftBox
    $Count++

    #--[ Right Checkbox ]--
    Remove-Variable -Name "RadioButton$Count" -ErrorAction SilentlyContinue
    $Right = New-Object System.Windows.Forms.radiobutton -Property @{
        Location = new-object System.Drawing.Size($CbRight,$CbHeight)
        Size = new-object System.Drawing.Size($CbBox,$TextHeight)
        Text = $SiteList[$Count]
        Enabled = $true 
    }
    
    New-Variable -Name "RadioButton$Count" -value $Right
    $RightBox = Get-Variable -name "RadioButton$Count" -ValueOnly
    $Range += $RightBox
    $Count++ 

    $CbHeight = $CbHeight+$CbVar
}

write-host "site count " $Count

$GroupBox.Location = '35,95'
$GroupBox.size = '310,135'
$GroupBox.text = "Locations"
$GroupBox.Controls.AddRange($Range)
$form.controls.add($GroupBox)

$BoxLength = 100
$LineLoc = 30
$InfoBox = New-Object System.Windows.Forms.TextBox
$InfoBox.Location = New-Object System.Drawing.Size((($FormHCenter-($BoxLength/2))-10),$LineLoc)
$InfoBox.Size = New-Object System.Drawing.Size($BoxLength,$TextHeight) 
$InfoBox.Enabled = $False
$InfoBox.TextAlign = 2
$Form.Controls.Add($InfoBox) #>

$BoxLength = 100
$LineLoc = $FormHeight-77
$CloseButton = new-object System.Windows.Forms.Button
$CloseButton.Location = New-Object System.Drawing.Size(($FormHCenter-($BoxLength/2)-75),$LineLoc)
$CloseButton.Size = new-object System.Drawing.Size($BoxLength,$ButtonHeight)
$CloseButton.TabIndex = 1
$CloseButton.Text = "Cancel/Close"
$CloseButton.Add_Click({
    $Form.Close()
    $Form.Dispose()
})
$Form.Controls.Add($CloseButton)

$ProcessButton = new-object System.Windows.Forms.Button
$ProcessButton.Location = new-object System.Drawing.Size(($FormHCenter-($BoxLength/2)+55),$LineLoc)
$ProcessButton.Size = new-object System.Drawing.Size($BoxLength,$ButtonHeight)
$ProcessButton.Enabled      = $false #true
$ProcessButton.Text = "Execute"
$Form.Controls.Add($ProcessButton)

$event1={
    if ($RadioButton0.Checked){$InfoBox.Text = "You selected 1"}
    if ($RadioButton1.Checked){$InfoBox.Text = "You Selected 2"}
    if ($RadioButton2.Checked){$InfoBox.Text = "You selected 3"}
    if ($RadioButton3.Checked){$InfoBox.Text = "You Selected 4"}
    if ($RadioButton4.Checked){$InfoBox.Text = "You Selected 5"}
    if ($RadioButton5.Checked){$InfoBox.Text = "You Selected 6"}
    if ($RadioButton6.Checked){$InfoBox.Text = "You Selected 7"}
    if ($RadioButton7.Checked){$InfoBox.Text = "You Selected 8"}
}

$RadioButton0.Add_Click($event1)
$RadioButton1.Add_Click($event1)
$RadioButton2.Add_Click($event1)
$RadioButton3.Add_Click($event1)
$RadioButton4.Add_Click($event1)
$RadioButton5.Add_Click($event1)
$RadioButton6.Add_Click($event1)
$RadioButton7.Add_Click($event1)
#>

# Where-Object {$PSItem -is [system.windows.controls.radiobutton] -and $PSItem.IsChecked} | Select-Object Name

<#
$Form.Controls | Where-Object { $Item -is [System.Windows.Forms.RadioButton] } | ForEach-Object { 
        $Item.Add_Click({ 
        If (-Not $ProcessButton.Enabled){
            $ProcessButton.Enabled = $True
        }
    }) 
}#>

$Form.topmost = $true
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
powershell forms winforms user-interface radiobuttonlist
1个回答
0
投票

感谢两位的回复。 通过审查这两个选项,我找到了一个可行的选项。 我不知道在控件的事件处理程序中,“$this”自动变量引用控件本身,并且可用于标识控件中的各种内容。 我不是 .Net 程序员,因此我通过检查其他脚本找到了许多这些东西,但我正在学习。 我还找到了一些很棒的资源来帮助我自己找到这些答案。

无论如何,我都能够替换原始 while 循环中的左右按钮创建,如下所示:

While ($SiteList.Count -gt $Count) {    
    #--[ Create Radio Boxes ]--
    Remove-Variable -Name "RadioButton$Count" -ErrorAction SilentlyContinue
    New-Variable -Name "RadioButton$Count" -value $RadBox
    $RadBox = Get-Variable -name "RadioButton$Count" -ValueOnly    
    $RadBox = new-object System.Windows.Forms.radiobutton
    $RadBox.Text = $SiteList[$Count]
    If (0,2,4,6,8 -contains "$Count"[-1]-48) {
        $RadBox.Location = new-object System.Drawing.Size($CbLeft,$CbHeight)
        $Count++
    }Else{
        $RadBox.Location = new-object System.Drawing.Size($CbRight,$CbHeight)
        $CbHeight = $CbHeight+$CbVar
        $Count++
    }
    $RadBox.Size = new-object System.Drawing.Size($CbBox,$TextHeight)
    $RadBox.Enabled = $true 
    $RadBox.Add_Click({
        $InfoBox.Text = $This.text
        $ProcessButton.Enabled = $true
    })
    $Range += $RadBox
}

这会在组框中动态生成并排按钮列表,并在信息框中显示单选按钮名称。 这是我的最终目标,以便能够提取所选按钮的编号。 此外,我还能够将循环代码与我读到的其他一些内容削减一半。

© www.soinside.com 2019 - 2024. All rights reserved.