在Powershell中格式化ListView用户选择
我正在尝试将用户的选择从ListView输出到消息框以进行确认 当我在powershell控制台中写入在Powershell中格式化ListView用户选择,listview,powershell,Listview,Powershell,我正在尝试将用户的选择从ListView输出到消息框以进行确认 当我在powershell控制台中写入$ListView.SelectedItems.Selection'时,选择以列表格式显示 1 2 3 如果我尝试在消息框中显示相同的输出,[System.Windows.Forms.Messagebox]::Show(“用户已选择:$($ListView.SelectedItems.Selection')”) 输出结果如下所示 用户已选择:1 2 3 类似地,在powershell控制台中使
$ListView.SelectedItems.Selection'
时,选择以列表格式显示
1
2
3
如果我尝试在消息框中显示相同的输出,[System.Windows.Forms.Messagebox]::Show(“用户已选择:$($ListView.SelectedItems.Selection')”)
输出结果如下所示
用户已选择:1 2 3
类似地,在powershell控制台中使用Write host$ListView.SelectedItems.“Selection”
也会在一行中输出所有内容
如何设置格式,以便插入逗号或句点,或者可能将其显示为列表,而不是将其全部显示为内联。当您在字符串中展开数据简单类型数组时,如下所示:
"$(@(1,2,3,4))"
PowerShell使用存储在Automatic变量$OFS
中的o输出f字段s分隔符来连接值。如果$OFS
为$null
,则分隔符默认为空格,这从您的问题中可以明显看出
在调用MessageBox.Show()之前,请更改$OFS
的值:
或者自己使用-join
进行连接:
$message = "User has selected: $($ListView.SelectedItems.'Selection' -join ', ')"
[System.Windows.Forms.Messagebox]::Show($message)
$($ListView.SelectedItems.Selection-join“,”)
$message = "User has selected: $($ListView.SelectedItems.'Selection' -join ', ')"
[System.Windows.Forms.Messagebox]::Show($message)