【发布时间】:2021-11-07 10:30:30
【问题描述】:
我正在尝试创建一个脚本,让用户从下拉列表中选择服务器。每个服务器都映射到一个唯一的数组,该数组进入一个 foreach 循环。循环遍历数组,并在表单上打印一个带有数组中值的复选框。这没有问题。问题是当我从下拉列表中选择不同的服务器并单击“选择服务器”按钮时,数组中的新值不会覆盖现有值。换句话说,表单上的复选框值不会使用新的数组值进行更新。我希望发生的是,当您单击“选择服务器”按钮时,复选框值会更新以反映与其相应服务器关联的数组值。
这是一个例子。
- 从下拉列表中选择 ServerA
- 选择“选择服务器”
- 以下复选框将在复选框的表单中列出:@('Zero','One','Two','Three')
- 现在,如果您单击 ServerB 并选择“选择服务器”,我希望新的复选框会用这些值覆盖现有的复选框:@('0','1','2','3')李>
不幸的是,这些值没有更新。 I need to have the array values update when the "Select Server" button is selected... Ive looked around at forums and have found some possible solutions but they all seems to fall short.
提前谢谢你。
function GenerateForm
{
$PC=
{
$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
# Make sure to vertically space them dynamically, counter comes in handy
$System_Drawing_Point.Y = 200 + (($CheckBoxCounter - 1) * 31) #Controls location on Y axis
$CheckBox.Location = $System_Drawing_Point
$CheckBox.DataBindings.DefaultDataSourceUpdateMode = 0
# Give it a unique name based on our counter
$CheckBox.Name = "CheckBox$CheckBoxCounter"
$form1.Controls.Add($CheckBox)
# return object ref to array
$Global:CheckBox
# increment our counter
$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)
######### Select Server Button
$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 # 0
$System_Drawing_Point.Y = 150 #150
$SelectPC.Location = $System_Drawing_Point
$SelectPC.DataBindings.DefaultDataSourceUpdateMode = 0
$SelectPC.add_Click($PC)
$form1.Controls.Add($SelectPC)
$result = $form1.ShowDialog()
$result
}
GenerateForm
【问题讨论】:
-
您是说第一次选择服务器时它可以工作,但之后就不行了?您永远不会设置任何
CheckBox的Checked或CheckState属性。每次选择服务器时,您都在现有控件之上添加新控件,而无需删除或重用已有的控件。我相信$CheckBox.Name = "CheckBox$CheckBoxCounter"也会导致名称冲突,因为每次$CheckBoxCounter都从1开始。 -
正确,如果我选择 ServerA 或 ServerB,复选框将生成相应的数组。但是,如果我随后选择一个新服务器,它不会替换表单上的现有阵列。你有没有机会分享一个关于我如何解决这个问题的例子?
-
我明白了,从第一次加载开始,您仍然看到相同的
Text。不是每次调用Click/$PC时都创建CheckBoxes,而是希望在创建$form1之后创建它们一次(与使用$dropdown和$SelectPC的方式相同)并存储它们在单独的变量中,或者更好的是,一个数组。那么当Click/$PC被调用时,你只需设置他们的Text和我想Checked属性。 -
你能分享一个我可以使用的例子吗?这对我有很大帮助。
标签: arrays powershell winforms checkbox powershell-5.0