我正在嘗試創建一個腳本,讓用戶從下拉串列中選擇一個服務器。每個服務器都映射到一個唯一的陣列,該陣列進入 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
uj5u.com熱心網友回復:
根據注釋中為每個主機容納可變數量的屬性的請求,首先我們將創建一個Panel來保存我們的CheckBoxes 并將其添加到我們的Form...
# Create a Panel to contain the dynamic collection of CheckBoxes
$HostPropertiesPanel = New-Object -TypeName 'System.Windows.Forms.Panel' -Property @{
# To illustrate the changing Size of the Panel
BackColor = [System.Drawing.Color]::GreenYellow
Location = New-Object -TypeName 'System.Drawing.Point' -Property @{
X = 27
Y = 200
}
Name = 'HostPropertiesPanel'
Size = [System.Drawing.Size]::Empty
}
$form1.Controls.Add($HostPropertiesPanel)
這消除了CheckBox我們自己跟蹤es的需要,因為我們會知道它們都包含在 this 中Panel。在Size最初是Empty因為沒有CheckBox到主機選擇下載到顯示幕。
在Click/內部,$PC然后我們將更改我們的主機屬性的定義方式...
# Set $hostProperties to a Hashtable array for the corresponding value of $hostname
# The IsChecked values are arbitrary for demonstration purposes
#TODO: Replace if...elseif with a switch statement; see "help about_Switch"
$hostProperties = if ($hostname -eq "ServerA") {
@{ Label = 'Zero'; IsChecked = $false },
@{ Label = 'One'; IsChecked = $true },
@{ Label = 'Two'; IsChecked = $true },
@{ Label = 'Three'; IsChecked = $false }
}
elseif ($hostname -eq "ServerB") {
@{ Label = '0'; IsChecked = $true },
@{ Label = '1'; IsChecked = $false },
@{ Label = '2'; IsChecked = $false },
@{ Label = '3'; IsChecked = $true }
}
elseif ($hostname -eq "ServerC") {
@{ Label = 'A'; IsChecked = $true },
@{ Label = 'B'; IsChecked = $true },
@{ Label = 'C'; IsChecked = $false }
}
elseif ($hostname -eq "ServerD") {
# Create a property (Hashtable) for each day of the week
[Enum]::GetNames([DayOfWeek]) | ForEach-Object -Process {
@{
Label = $_
# Check the box if the day name has an odd number of vowels
IsChecked = [Regex]::Matches($_, '[aeiou]').Count % 2 -eq 1
}
}
}
else {
# Oops! A host with no properties defined was selected...
}
我們不是使用一個陣列來存盤標簽,使用另一個來存盤CheckBox狀態,而是Hashtable根據已選擇的主機獲取一組s 并將其存盤在$hostProperties.
在我們創建新的CheckBoxes之前,我們必須從中洗掉任何現有的 es$HostPropertiesPanel并根據Length所選主機的屬性調整其大小...
# Remove all existing CheckBoxes from the Panel
$HostPropertiesPanel.Controls.Clear()
# Resize the Panel to accommodate the new count of host properties
$HostPropertiesPanel.Size = New-Object -TypeName 'System.Drawing.Size' -Property @{
Width = 104
Height = if ($hostProperties.Length -gt 0) {
($hostProperties.Length - 1) * 31 24
}
else {
0
}
}
然后我們只需要調整創建和配置每個CheckBox...
for ($index = 0; $index -lt $hostProperties.Length; $index ) {
$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 = $hostProperties[$index].Label
$CheckBox.Checked = $hostProperties[$index].IsChecked
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 0
# Make sure to vertically space them dynamically, counter comes in handy
$System_Drawing_Point.Y = $index * 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$index"
$HostPropertiesPanel.Controls.Add($CheckBox)
}
關鍵的變化是使用in的當前檢索new的Text和Checked值。CheckBox$index$hostProperties
最后的更改是更新主機名串列...
$dropdown.Items.AddRange(
@('ServerA', 'ServerB', 'ServerC', 'ServerD', 'ServerX')
)
$form1.Controls.Add($dropdown)
完整的腳本看起來像這樣......
function GenerateForm {
$PC = {
$hostname = $dropdown.SelectedItem
# Set $hostProperties to a Hashtable array for the corresponding value of $hostname
# The IsChecked values are arbitrary for demonstration purposes
#TODO: Replace if...elseif with a switch statement; see "help about_Switch"
$hostProperties = if ($hostname -eq "ServerA") {
@{ Label = 'Zero'; IsChecked = $false },
@{ Label = 'One'; IsChecked = $true },
@{ Label = 'Two'; IsChecked = $true },
@{ Label = 'Three'; IsChecked = $false }
}
elseif ($hostname -eq "ServerB") {
@{ Label = '0'; IsChecked = $true },
@{ Label = '1'; IsChecked = $false },
@{ Label = '2'; IsChecked = $false },
@{ Label = '3'; IsChecked = $true }
}
elseif ($hostname -eq "ServerC") {
@{ Label = 'A'; IsChecked = $true },
@{ Label = 'B'; IsChecked = $true },
@{ Label = 'C'; IsChecked = $false }
}
elseif ($hostname -eq "ServerD") {
# Create a property (Hashtable) for each day of the week
[Enum]::GetNames([DayOfWeek]) | ForEach-Object -Process {
@{
Label = $_
# Check the box if the day name has an odd number of vowels
IsChecked = [Regex]::Matches($_, '[aeiou]').Count % 2 -eq 1
}
}
}
else {
# Oops! A host with no properties defined was selected...
}
# Don't execute any layout logic until all changes are complete
$HostPropertiesPanel.SuspendLayout()
# Remove all existing CheckBoxes from the Panel
$HostPropertiesPanel.Controls.Clear()
# Resize the Panel to accommodate the new count of host properties
$HostPropertiesPanel.Size = New-Object -TypeName 'System.Drawing.Size' -Property @{
Width = 104
Height = if ($hostProperties.Length -gt 0) {
($hostProperties.Length - 1) * 31 24
}
else {
0
}
}
for ($index = 0; $index -lt $hostProperties.Length; $index ) {
$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 = $hostProperties[$index].Label
$CheckBox.Checked = $hostProperties[$index].IsChecked
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 0
# Make sure to vertically space them dynamically, counter comes in handy
$System_Drawing_Point.Y = $index * 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$index"
$HostPropertiesPanel.Controls.Add($CheckBox)
}
# All changes are complete, so resume layout logic
$HostPropertiesPanel.ResumeLayout()
}
$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
$dropdown.Items.AddRange(
@('ServerA', 'ServerB', 'ServerC', 'ServerD', 'ServerX')
)
$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)
# Create a Panel to contain the dynamic collection of CheckBoxes
$HostPropertiesPanel = New-Object -TypeName 'System.Windows.Forms.Panel' -Property @{
# To illustrate the changing Size of the Panel
BackColor = [System.Drawing.Color]::GreenYellow
Location = New-Object -TypeName 'System.Drawing.Point' -Property @{
X = 27
Y = 200
}
Name = 'HostPropertiesPanel'
Size = [System.Drawing.Size]::Empty
}
$form1.Controls.Add($HostPropertiesPanel)
$name = New-Object System.Windows.Forms.Label -Property @{
Text = "Start Time"
Location = "900, 220"
ForeColor = "Black"
Height = 22
Width = 200
}
$form1.Controls.Add($name)
$result = $form1.ShowDialog()
$result
}
GenerateForm
另一種方法,特別是對于大量宿主屬性,是將Panel及其內容替換為CheckedListBox.
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/351180.html
標籤:数组 电源外壳 winforms 复选框 powershell-5.0
上一篇:Powershell:為什么("111"-match"111-XD")回傳false而("111-XD"-match"111&qu
