powershell - 如何将Windows窗体文本框输入传递给powershell模块功能
问题描述
以下是我正在尝试做的抽象视图;所有这些脚本都在同一个文件夹中
# Windows Form script called gui.ps1
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = New-Object System.Drawing.Point(891, 414)
$Form.text = "Form"
$Form.TopMost = $false
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "Enter the Text : "
$Label1.AutoSize = $true
$Label1.width = 25
$Label1.height = 10
$Label1.location = New-Object System.Drawing.Point(39, 188)
$Label1.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10)
$text_box = New-Object system.Windows.Forms.TextBox
$text_box.multiline = $false
$text_box.width = 695
$text_box.height = 20
$text_box.location = New-Object System.Drawing.Point(144, 183)
$text_box.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10)
$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "Submit"
$Button1.width = 106
$Button1.height = 30
$Button1.location = New-Object System.Drawing.Point(386, 239)
$Button1.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10)
$Form.controls.AddRange(@($Label1, $text_box, $Button1))
$Button1.Add_Click({ txtInput }) # txtInput is defined in module.
# module called demo.psm1
function txtInput
{
$txt = $text_box.Text
Write-Host $txt
}
# execute.ps1
Import-Module -Name $PSScriptRoot\demo.psm1
Set-Location $PSScriptRoot
. .\gui.ps1 # dot sourcing
[void]$Form.ShowDialog()
当我运行 execute.ps1 脚本时,表单正在显示。但是当我单击 Button1 时,文本框输入未显示在 powershell 中。试图将模块中的 'txt' 变量定义为 $global:txt = $text_box.Text。那没起效。
我怎样才能让它工作?
解决方案
推荐阅读
- ios - 如何构建和运行现有的 react-native 应用程序?
- c# - 如何在 WPF 中检查 Mysql 表中是否存在值
- javascript - 如何检查用户是否发送了 json Web 令牌?
- android - 如何在android studio中使用自签名证书
- react-native - 在 React Native 中滚动以在屏幕上显示视图
- html - 如何将样式应用于 div 但不是它包含的样式
- database - 如何仅选择未输入日期的字段?
- php - 如果字段失败,提交后在 laravel 中动态保持数据表单
- windows - 工作线程不屈服于主线程
- c - 如何让函数在 C 中返回自己的类型?