Default value on set
kalivodv opened this issue Β· comments
ViktorK commented
Hi Donald,
I would like to thank you for AnyBox module, it's really great π and I have decided to use it in one of my projects :)
Still, I am experiencing one issue. If I try to set parameter -DefaultValue
together with -ValidateSet
and -ShowSetAs ComboBox
, I am not getting the default value. With -ShowSetAs Radio
, default value is working as expected.
$prompts = @(
(New-AnyBoxPrompt -InputType Text -Message 'Username'),
(New-AnyBoxPrompt -ValidateSet @('group1', 'group2', 'group3') -ShowSetAs ComboBox -DefaultValue 'group2' -Message 'Group'),
(New-AnyBoxPrompt -ValidateSet @('5 GB', '10 GB') -ShowSetAs Radio -DefaultValue '10 GB' -Message 'Quota')
)
$buttons = @(
'Save',
'Quit'
)
$answer = Show-AnyBox -Title 'New User' -Prompts $prompts -Buttons $buttons
Could you please help me with this issue?
Regards
Viktor
ViktorK commented
Unfortunately, I wasn't able to solve this. But I found temporary solution.
As workaround:
- I have added index numbers to every object in all of my arrays
- changed one line in
Show-AnyBox.ps1
that seems to be problematic - underif ($prmpt.ShowSetAs -eq [AnyBox.SetPresentation]::ComboBox)
:
if ($prmpt.DefaultValue) {
$inBox.SelectedItem = $prmpt.DefaultValue
}
changed to:
if ($prmpt.DefaultValue) {
$inBox.SelectedIndex = $prmpt.DefaultValue
}
So the working example now looks like:
$groups = @('group1', 'group2', 'group3')
$i = -1
$groupsarray = @()
foreach ($one in $groups){
$i++
$groupsobject = New-Object PSobject
$groupsobject | Add-Member -MemberType NoteProperty -Name Name -Value $one
$groupsobject | Add-Member -MemberType NoteProperty -Name Nr -Value $i
$groupsarray += $groupsobject
}
$defaultgroup = "group2"
$defaultgroupindex = ($groupsarray | Where-Object {$_.Name -eq $defaultgroup}).Nr
$prompts = @(
(New-AnyBoxPrompt -InputType Text -Message 'Username'),
(New-AnyBoxPrompt -ValidateSet $groupsarray.Name -DefaultValue $defaultgroupindex -Message 'Group'),
(New-AnyBoxPrompt -ValidateSet @('5 GB', '10 GB') -ShowSetAs Radio -DefaultValue '10 GB' -Message 'Quota')
)
$buttons = @(
'Save',
'Quit'
)
$answer = Show-AnyBox -Title 'New User' -Prompts $prompts -Buttons $buttons
I know it's crazy a bit, but I was really hopeless π
Viktor