我的表格上有一个ComboBox
控件。我想要的是,当我将ComboBox
中的项目从一个将事件更改为另一个项目时。在选择同一元素时,请至关重要。一直以来,我一直使用ComboBox.Add_SelectionChangeCommitted($function)
,但是很快我意识到,当从列表中选择相同的(选定)项目时,处理程序也执行的块。在Combobox事件中进行一些挖掘,我完全困惑。尝试了几个事件(SelectedItemChanged
,SelectedIndexChanged
),我永远无法实现所需的结果。
选择相同元素时我想做的事情以及几次不应该做的示例。当执行手册代码块几次时,所有文本框的内容都会清除,但我不想。
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '420,240'
$Form.TopMost = $false
$Form.FormBorderStyle = 'Fixed3D'
$Form.MaximizeBox = $false
$ComboBox = New-Object system.Windows.Forms.ComboBox
$ComboBox.width = 391
$ComboBox.height = 47
@('Automatic (DHCP)','Manual Input') | ForEach-Object {[void] $ComboBox.Items.Add($_)}
$ComboBox.location = New-Object System.Drawing.Point(13,57)
$ComboBox.DropDownStyle = 'DropDownList'
$ComboBox.DrawMode = 'OwnerDrawFixed'
$Button = New-Object system.Windows.Forms.Button
$Button.text = "Set"
$Button.width = 60
$Button.height = 30
$Button.visible = $false
$Button.enabled = $false
$Button.location = New-Object System.Drawing.Point(336,126)
$CheckBox = New-Object system.Windows.Forms.CheckBox
$CheckBox.width = 65
$CheckBox.height = 15
$CheckBox.visible = $false
$CheckBox.Checked = $false
$CheckBox.enabled = $true
$CheckBox.location = New-Object System.Drawing.Point(16,210)
$TextBox1 = New-Object system.Windows.Forms.TextBox
$TextBox1.Name = "TextBox1"
$TextBox1.multiline = $false
$TextBox1.width = 40
$TextBox1.height = 20
$TextBox1.visible = $false
$TextBox1.enabled = $true
$TextBox1.location = New-Object System.Drawing.Point(81,100)
$TextBox2 = New-Object system.Windows.Forms.TextBox
$TextBox2.Name = "TextBox2"
$TextBox2.multiline = $false
$TextBox2.width = 40
$TextBox2.height = 20
$TextBox2.visible = $false
$TextBox2.enabled = $false
$TextBox2.location = New-Object System.Drawing.Point(81,208)
$Form.controls.AddRange(@($TextBox1,$TextBox2,$Button,$ComboBox,$CheckBox))
$global:ManualChecked = $null
$global:AutomaticChecked = $null
$ComboBox.Add_SelectionChangeCommitted($methodSelection)
$netwValues = New-Object 'System.Collections.Hashtable'
$methodSelection =
{
switch($ComboBox.Text)
{
"Manual Input"
{
$CheckBox.Visible = $Button.Visible = $true
$Button.Enabled = $false
ForEach ($control in $Form.controls)
{
if ($control -is [System.Windows.Forms.TextBox] )
{
$control.Visible = $control.Enabled = $true
$control.Clear()
if($netwValues.Count -gt 0)
{
$control.Text = $netwValues.Item($control.Name)
$netwValues.Remove($control.Name)
}
}
}
if($global:ManualChecked -eq 1)
{
$CheckBox.Checked = $true
$TextBox2.Enabled = $true
}
else
{
$CheckBox.Checked = $false
$TextBox2.Enabled = $false
}
}
"Automatic (DHCP)"
{
ForEach($control in $Form.controls)
{
if($control -is [System.Windows.Forms.TextBox] -or $control -is [System.Windows.Forms.CheckBox] -or $control -is [System.Windows.Forms.Button])
{
$control.Visible = $control.Enabled = $true
if($control -is [System.Windows.Forms.Label] -or $control -is [System.Windows.Forms.TextBox])
{
$control.Enabled = $false
if($control -is [System.Windows.Forms.TextBox])
{
if($control.Text.Length)
{
$netwValues.Add($control.Name,$control.Text)
$control.Clear()
}
}
}
}
}
if($global:AutomaticChecked -eq 1)
{
$CheckBox.Checked = $true
}
else
{
$CheckBox.Checked = $false
}
}
}
}
您可以自己实现此逻辑。
# Simple class for using as item for combobox instead of just string.
class CbItem
{
CbItem([string] $value)
{
$this.Value = $value
}
[string] $Value
[string] ToString()
{
return $this.Value
}
}
# Add instances of this class into combobox.
$ComboBox.Items.Add([CbItem]::new('Automatic (DHCP)'))
$ComboBox.Items.Add([CbItem]::new('Manual Input'))
# Create a variable for storing current combobox's item.
$global:CurrentCbItem = $null
# Add logic at the begining of handler for exiting if selected item is the same as the last one.
$methodSelection =
{
if ([Object]::ReferenceEquals($global:CurrentCbItem, $ComboBox.SelectedItem))
{
return
}
$global:CurrentCbItem = $ComboBox.SelectedItem
...
}