从文本框中取回值 class
Get value back from a text box class
我们有一个脚本,它是由一个已不在我们身边的人编写的。我是 powershell
的新手,这个函数没有返回值:
# prompt user for ip/dns address input.
Function get-ip
{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
# Creates a message box that accepts dns/ip address input.
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "User Input Required"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$x=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$x=$objTextBox.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,40)
$objLabel.Text = "Please enter the IP address of the server you want to connect to:"
$objForm.Controls.Add($objLabel)
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(10,70)
$objTextBox.Size = New-Object System.Drawing.Size(260,20)
$objForm.Controls.Add($objTextBox)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
}
我是这样称呼它的:
$ip = get-ip
我输入了一个值,但我在 $ip
中没有得到任何东西。
如何捕获文本框的值?
马丁
我按照 Martin 的建议进行了更改,这是我的新代码:
# prompt user for ip/dns address input.
Function get-ip
{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
# Creates a message box that accepts dns/ip address input.
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "User Input Required"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$script:x=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$script:x=$objTextBox.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,40)
$objLabel.Text = "Please enter the IP address of the server you want to connect to:"
$objForm.Controls.Add($objLabel)
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(10,70)
$objTextBox.Size = New-Object System.Drawing.Size(260,20)
$objForm.Controls.Add($objTextBox)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
$x
}
我收到与之前相同的错误消息:
PS E:\Dropbox\Powershell Scripts\SSS Cloud Icons> .\cloudicons.ps1
Creating Directories...
mkdir : Cannot bind argument to parameter 'Path' because it is an empty string.
At E:\Dropbox\Powershell Scripts\SSS Cloud Icons\cloudicons.ps1:80 char:4
+ md $_.userName
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [mkdir], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,mkdir
我是不是漏掉了什么?
** 更新 **
有人问我如何调用 get-ip:
# Insert user entered IP into pipeline.
$ip = get-ip
# Sets user entered IP into new file called propanehasip.rdp.
(get-content .\propanetest.rdp) -replace 'full address:s:INSERTIPHERE',"full address:s:$ip" | Out-File propanehasip.rdp
Write-Host "Creating Directories..."
Import-Csv $csv | ForEach-Object {
# Creates directories based on the userName field.
md $_.userName
# Creates icons based on propanehasip.rdp and WSID fields.
(get-content ".\propanehasip.rdp") -replace 'remoteapplicationcmdline:s:INSERTWSIDHERE',"remoteapplicationcmdline:s:$($_.wsid1)" | out-file ".$($_.username)\Propane $($_.wsid1).rdp"
(get-content ".\propanehasip.rdp") -replace 'remoteapplicationcmdline:s:INSERTWSIDHERE',"remoteapplicationcmdline:s:$($_.wsid2)" | out-file ".$($_.username)\Propane $($_.wsid2).rdp"
}
}
更新 #2
我在 visual studio 中找到了调试方法。错误发生在这一行:
(get-content .\propanetest.rdp) -替换 'full address:s:INSERTIPHERE',"full address:s:$ip" |外文件 propanehasip_new.rdp
$x 仅在块内有效(变量$x 的范围),如果将其更改为$script:x 并在函数的结尾} 之前添加一个$x,则可以使用。
需要额外的 $x 来表示函数的 return 值。
更改:
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$script:x=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$script:x=$objTextBox.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)
函数结束:
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
$x
}
并且...此脚本取自 here,应该演示 Windows.Forms 的用法。但是,我没有找到发布日期。
我们有一个脚本,它是由一个已不在我们身边的人编写的。我是 powershell
的新手,这个函数没有返回值:
# prompt user for ip/dns address input.
Function get-ip
{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
# Creates a message box that accepts dns/ip address input.
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "User Input Required"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$x=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$x=$objTextBox.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,40)
$objLabel.Text = "Please enter the IP address of the server you want to connect to:"
$objForm.Controls.Add($objLabel)
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(10,70)
$objTextBox.Size = New-Object System.Drawing.Size(260,20)
$objForm.Controls.Add($objTextBox)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
}
我是这样称呼它的:
$ip = get-ip
我输入了一个值,但我在 $ip
中没有得到任何东西。
如何捕获文本框的值?
马丁
我按照 Martin 的建议进行了更改,这是我的新代码:
# prompt user for ip/dns address input.
Function get-ip
{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
# Creates a message box that accepts dns/ip address input.
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "User Input Required"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$script:x=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$script:x=$objTextBox.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,40)
$objLabel.Text = "Please enter the IP address of the server you want to connect to:"
$objForm.Controls.Add($objLabel)
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(10,70)
$objTextBox.Size = New-Object System.Drawing.Size(260,20)
$objForm.Controls.Add($objTextBox)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
$x
}
我收到与之前相同的错误消息:
PS E:\Dropbox\Powershell Scripts\SSS Cloud Icons> .\cloudicons.ps1
Creating Directories...
mkdir : Cannot bind argument to parameter 'Path' because it is an empty string.
At E:\Dropbox\Powershell Scripts\SSS Cloud Icons\cloudicons.ps1:80 char:4
+ md $_.userName
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [mkdir], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,mkdir
我是不是漏掉了什么?
** 更新 **
有人问我如何调用 get-ip:
# Insert user entered IP into pipeline.
$ip = get-ip
# Sets user entered IP into new file called propanehasip.rdp.
(get-content .\propanetest.rdp) -replace 'full address:s:INSERTIPHERE',"full address:s:$ip" | Out-File propanehasip.rdp
Write-Host "Creating Directories..."
Import-Csv $csv | ForEach-Object {
# Creates directories based on the userName field.
md $_.userName
# Creates icons based on propanehasip.rdp and WSID fields.
(get-content ".\propanehasip.rdp") -replace 'remoteapplicationcmdline:s:INSERTWSIDHERE',"remoteapplicationcmdline:s:$($_.wsid1)" | out-file ".$($_.username)\Propane $($_.wsid1).rdp"
(get-content ".\propanehasip.rdp") -replace 'remoteapplicationcmdline:s:INSERTWSIDHERE',"remoteapplicationcmdline:s:$($_.wsid2)" | out-file ".$($_.username)\Propane $($_.wsid2).rdp"
}
}
更新 #2
我在 visual studio 中找到了调试方法。错误发生在这一行:
(get-content .\propanetest.rdp) -替换 'full address:s:INSERTIPHERE',"full address:s:$ip" |外文件 propanehasip_new.rdp
$x 仅在块内有效(变量$x 的范围),如果将其更改为$script:x 并在函数的结尾} 之前添加一个$x,则可以使用。 需要额外的 $x 来表示函数的 return 值。
更改:
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$script:x=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$script:x=$objTextBox.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)
函数结束:
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
$x
}
并且...此脚本取自 here,应该演示 Windows.Forms 的用法。但是,我没有找到发布日期。