You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PowerShell实现表单复选框随选中服务器动态刷新的问题求助

问题修复方案

根因说明

  • 每次触发按钮点击事件生成复选框前,没有清理之前生成的旧复选框,新旧控件重叠导致看起来没有更新效果
  • 原代码存在语法错误:$form1.Controls.Add($hostname),$hostname是字符串类型的服务器名,不是可添加的控件对象,运行时会抛出异常

修复逻辑

在按钮点击事件的最开始添加清理逻辑:遍历表单所有控件,匹配命名规则为CheckBox+数字的复选框,全部从表单控件集合中移除后,再生成新的复选框即可实现覆盖更新的效果。

修正后完整代码

function GenerateForm 
{   
$PC=
{
    # 新增:清理之前生成的旧复选框
    $form1.Controls | Where-Object { $_.Name -like 'CheckBox*' } | ForEach-Object {
        $form1.Controls.Remove($_)
    }

    $hostname = $dropdown.SelectedItem
    
    if ($hostname -eq "ServerA")
    { 
        $CheckBoxLabels = @('Zero','One','Two','Three')
    }
    elseif($hostname -eq "ServerB")
    {
        $CheckBoxLabels = @('0','1','2','3')
    }
        
    $name = New-Object System.Windows.Forms.Label -Property @{
        Text = "Start Time"
        Location = "900, 220"
        ForeColor = "Black"
        Height = 22
        Width = 200
    }
    # 移除错误代码:$form1.Controls.Add($hostname)
    
    $CheckBoxCounter = 1
    
    $CheckBoxes = foreach($Label in $CheckBoxLabels) 
    {   
        $CheckBox = New-Object System.Windows.Forms.CheckBox        
        $CheckBox.UseVisualStyleBackColor = $True
        $System_Drawing_Size = New-Object System.Drawing.Size
        $System_Drawing_Size.Width = 104
        $System_Drawing_Size.Height = 24
        $CheckBox.Size = $System_Drawing_Size
        $CheckBox.TabIndex = 2
    
        $CheckBox.Text = $Label
        
        $System_Drawing_Point = New-Object System.Drawing.Point
        $System_Drawing_Point.X = 27
        $System_Drawing_Point.Y = 200 + (($CheckBoxCounter - 1) * 31)
        $CheckBox.Location = $System_Drawing_Point
        $CheckBox.DataBindings.DefaultDataSourceUpdateMode = 0

        $CheckBox.Name = "CheckBox$CheckBoxCounter"

        $form1.Controls.Add($CheckBox)

        $Global:CheckBox
        $CheckBoxCounter++
    }
}

    $form1 = New-Object System.Windows.Forms.Form
    $form1.Text = "UCCE Log Collector - Version 2.0"
    $form1.Name = "form1"
    $form1.DataBindings.DefaultDataSourceUpdateMode = 0
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 1150
    $System_Drawing_Size.Height = 500
    $form1.ClientSize = $System_Drawing_Size

$dropdown = New-Object System.Windows.Forms.ListBox
$dropdown.Location = New-Object System.Drawing.Point(10,50)
$dropdown.Size = New-Object System.Drawing.Size(100,20)
$dropdown.Height = 80

[void] $dropdown.Items.Add('ServerA')
[void] $dropdown.Items.Add('ServerB')

$form1.Controls.Add($dropdown)

$SelectPC = New-Object System.Windows.Forms.Button
$SelectPC.TabIndex = 4
$SelectPC.Name = "SelectPC"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 120
$System_Drawing_Size.Height = 30
$SelectPC.Size = $System_Drawing_Size
$SelectPC.UseVisualStyleBackColor = $True
$SelectPC.Text = "Select Server"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 0
$System_Drawing_Point.Y = 150
$SelectPC.Location = $System_Drawing_Point
$SelectPC.DataBindings.DefaultDataSourceUpdateMode = 0
$SelectPC.add_Click($PC)

$form1.Controls.Add($SelectPC)

$result = $form1.ShowDialog()
$result

}
GenerateForm

内容的提问来源于stack exchange,提问作者user6534872

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 04:36:05