命令行$X = 3927,3988,4073,4151
看起来很棒,
PS C:\Users\Administrator> $X
3927
3988
4073
4151
字符串
但是如果我从一个表单中读取它,它会返回3927,3988,4073,4151
我应该使用$XBox.Text
以外的东西吗?
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$Title = "Numeric ID"
$Form = New-Object "System.Windows.Forms.Form"
$Form.Width = 850
$Form.Height = 140
$Form.Text = $Title
$Form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
$XLabel = New-Object "System.Windows.Forms.Label";$XLabel.Left = 5;$XLabel.Top = 18;$XLabel.Text = 'My Numbers:'
$XBox = New-Object "System.Windows.Forms.TextBox";$XBox.Left = 105;$XBox.Top = 15;$XBox.width = 700
$DefaultValue = ""
$XBox.Text = $DefaultValue
$Button = New-Object "System.Windows.Forms.Button";$Button.Left = 400;$Button.Top = 45;$Button.Width = 100;$Button.Text = "Accept"
$EventHandler = [System.EventHandler]{
$XBox.Text
$Form.Close()
}
$Button.Add_Click($EventHandler)
$Form.Controls.Add($button)
$Form.Controls.Add($XLabel)
$Form.Controls.Add($XBox)
$Ret = $Form.ShowDialog()
$X = @($XBox.Text)
型
1条答案
按热度按时间cld4siwp1#
***
$X
包含一个由 numbers - number字面值组成的 array,这些字面值被解析为[int]
示例,并通过,
数组构造函数运算符组装成一个[object[]]
数组。字符串
$XBox.Text
包含一个 * 单一字符串 *,只是碰巧 * 看起来像 * 定义$X
数组的源代码。如果你想将这个字符串 * 解释为PowerShell源代码 *,以获得一个数字数组,你可以 * 使用
Invoke-Expression
,但出于安全原因,通常是ill-advised。更安全的方法是自己对字符串进行约束解释(即使数字周围有空格,这也可以工作):
型
输出(
[int]
值的数组):型