使用WinForms的Powershell - 将用户输入从函数返回到文本框中

时间:2018-03-09 11:51:28

标签: winforms powershell event-handling

我在powershell中为我的表单元素设置了一个单独的脚本,并使用点源将两者链接在一起。我正在调用文本框功能,这个函数应该将用户的输入传递回原始脚本,但事实并非如此。

---------- First Script - being used to call ---------- 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 


#---------- Overall Form Config ----------#
$form = New-Object System.Windows.Forms.Form
$form.Text = "New Service Account"
$form.Size = New-Object System.Drawing.Size(300,300) 
$form.StartPosition = "CenterScreen"



#---------- Account Name Field ----------#

."C:\Temp\include.ps1"
formLabel -labLocW 10 -labLocH 20 -labSizeW 280 -labSizeH 30 -labText "Enter SVC Account Name (Do Not Include SVC_)"
$a = formTextBox -tboxLocW 10 -tboxLocH 50 -tboxSizeW 260 -tboxSizeH 30
$b = formTextBox -tboxLocW 10 -tboxLocH 100 -tboxSizeW 260 -tboxSizeH 30
#$ok = formOKBut -okLocW 100 -okLocH 120 -okSizeW 60 -okSizeH 30 -okText "OK"

    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Point(100,120)
    $OKButton.Size = New-Object System.Drawing.Size(60,30)
    $OKButton.Text = "OK"
    $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    $form.AcceptButton = $OKButton
    $form.Controls.Add($OKButton)


if($okbutton.Clicked){
    Write-Host $a

}

#---------- Show Form ----------#
$showForm = $form.ShowDialog() 


---------- Dot Script include ---------- 
function formTextBox($tboxLocW,$tboxLocH,$tboxSizeW,$tboxSizeH) {
    $textBox = New-Object System.Windows.Forms.TextBox
    $textBox.Location = New-Object System.Drawing.Point($tboxLocW,$tboxLocH) 
    $textBox.Size = New-Object System.Drawing.Size($tboxSizeW,$tboxSizeH) 
    $form.Controls.Add($textBox)
    $output = $textBox.Text
    return $output
}

1 个答案:

答案 0 :(得分:2)

在您的formTextBox功能中,您甚至在对话框显示之前尝试返回文本框控件的文本所以不会工作。

而是从函数中返回控件,在关闭对话框后可以检查.Text属性

function formTextBox($tboxLocW,$tboxLocH,$tboxSizeW,$tboxSizeH) {
    $textBox = New-Object System.Windows.Forms.TextBox
    $textBox.Location = New-Object System.Drawing.Point($tboxLocW,$tboxLocH) 
    $textBox.Size = New-Object System.Drawing.Size($tboxSizeW,$tboxSizeH) 
    $form.Controls.Add($textBox)
    return $textBox # Return the text box itself (the object)
}

# ...

# Create the text box, add it to the form, and store it in variable $a.
$a = formTextBox -tboxLocW 10 -tboxLocH 50 -tboxSizeW 260 -tboxSizeH 30

# ...

# Invoke the dialog and act on how it was closed.
if ($form.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
    $textA = $a.Text  # Get the text box' text now.
    Write-Verbose -Verbose "OK clicked: text box `$a contains: $textA"
} else {
    Write-Verbose -Verbose "Dialog was closed without clicking OK."
}

此外,看起来您尝试使用以下代码对单击“确定”按钮的事件作出反应,这也不会起作用,因为它再一次在对话框显示之前执行同步

if($okbutton.Clicked){  # DOESN'T WORK - event delegate needed instead
    Write-Host $a    
}

相反,您需要设置事件处理程序(事件委托),在PowerShell中采用<control>.add_<event-name>({ ... }) 的形式;就目前的情况而言,我们想对Click event

作出反应
$OKButton.add_Click({
    param([object] $sender, [System.EventArgs] $evtArgs)
    Write-Verbose -Verbose "Button '$($sender.Text)' clicked."
})

当按下按钮时,现在将调用脚本块({ ... })。 param()块声明传递给您的事件委托的两个参数,但如果您不需要有关发起事件的人($sender)或详细信息,您可以完全省略声明。事件($evtArgs),如果有的话。

注意:要确定给定的[System.Windows.Forms.Control]类型的可用事件以及传递的特定[System.EventArgs]子类,您可以使用类似 New-Object System.Windows.Forms.Button | Get-Member -MemberType Event