我有一个脚本,用户从下拉列表中选择选项。但如果用户没有选择任何内容,我会收到错误。如何设置返回的默认值,即使用户没有输入值。
这是脚本
########################
# Edit This item to change the DropDown Values
[array]$DropDownArray = "c", "d", "share"
# This Function Returns the Selected Value and Closes the Form
function Return-DropDown {
$script:Choice = $DropDown.SelectedItem.ToString()
$Form.Close()
}
function selectShare{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form = New-Object System.Windows.Forms.Form
$Form.width = 300
$Form.height = 150
$Form.Text = ”DropDown”
$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(100,10)
$DropDown.Size = new-object System.Drawing.Size(130,30)
ForEach ($Item in $DropDownArray) {
[void] $DropDown.Items.Add($Item)
}
$Form.Controls.Add($DropDown)
$DropDownLabel = new-object System.Windows.Forms.Label
$DropDownLabel.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel.size = new-object System.Drawing.Size(100,40)
$DropDownLabel.Text = "Select shared folder to deploy SEP"
$Form.Controls.Add($DropDownLabel)
$Button = new-object System.Windows.Forms.Button
$Button.Location = new-object System.Drawing.Size(100,50)
$Button.Size = new-object System.Drawing.Size(100,20)
$Button.Text = "Select an Item"
$Button.Add_Click({Return-DropDown})
$form.Controls.Add($Button)
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
return $script:choice
}
$share = selectShare
write-host $share
如果用户没有从下拉菜单中选择
,则会出现错误You cannot call a method on a null-valued expression.
At D:\Script\comp.ps1:10 char:2
+ $script:Choice = $DropDown.SelectedItem.ToString()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
答案 0 :(得分:3)
为什么不这样做:
$DropDown.SelectedItem = $DropDown.Items[0]
在您出示表格之前?或者将其设置为=无论您的默认项目是什么。
修改或者更好的是,设置一个值检查功能,并在点击“确定”时调用它,以确保所有字段都有有效值。
答案 1 :(得分:2)
这就是我跳过此错误的方法:
[array]$DropDownArrayItems = "","Group1","Group2","Group3"
[array]$DropDownArray = $DropDownArrayItems | sort
# This Function Returns the Selected Value and Closes the Form
function Return-DropDown {
if ($DropDown.SelectedItem -eq $null){
$DropDown.SelectedItem = $DropDown.Items[0]
$script:Choice = $DropDown.SelectedItem.ToString()
$Form.Close()
}
else{
$script:Choice = $DropDown.SelectedItem.ToString()
$Form.Close()
}
}
function SelectGroup{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form = New-Object System.Windows.Forms.Form
$Form.width = 300
$Form.height = 150
$Form.Text = ”DropDown”
$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(100,10)
$DropDown.Size = new-object System.Drawing.Size(130,30)
ForEach ($Item in $DropDownArray) {
[void] $DropDown.Items.Add($Item)
}
$Form.Controls.Add($DropDown)
$DropDownLabel = new-object System.Windows.Forms.Label
$DropDownLabel.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel.size = new-object System.Drawing.Size(100,40)
$DropDownLabel.Text = "Select Group:"
$Form.Controls.Add($DropDownLabel)
$Button = new-object System.Windows.Forms.Button
$Button.Location = new-object System.Drawing.Size(100,50)
$Button.Size = new-object System.Drawing.Size(100,20)
$Button.Text = "Select an Item"
$Button.Add_Click({Return-DropDown})
$form.Controls.Add($Button)
$form.ControlBox = $false
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
return $script:choice
}
$Group = $null
$Group = SelectGroup
while ($Group -like ""){
$Group = SelectGroup
}
我还改进了这个下拉菜单,如删除控制框,按名称排序数组
答案 2 :(得分:0)
试试这个:
If ($DropDown.SelectedItem -ne $null)<br>
如果是$ null,请设置其他输出。