如何防止 Windows 表单文本框中的 null/whitespace 值(使用 Powershell)?

How to prevent null/whitespace value in Windows Form textbox (using Powershell)?

我制作了一个 Windows 表单(这是它的最后一部分),允许用户在文本框中输入搜索词。整个脚本检查服务器上的日志文件,并将它们下载到用户的机器上。文本框数据包含一个字符串(日期、帐户编号等)...如果留空,它将被视为通配符,下载所选文件夹中的每个日志文件。我不确定是否可以在输入数据之前禁用 "ok" 按钮,或者提供一个 pop-up/message 框提示用户输入搜索词?我将之前的 code/variables 排除在该示例之外,因为它与该问题无关。感谢您提前提供的帮助!

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$form = New-Object System.Windows.Forms.Form 
$form.Text = "Enter search criteria"
$form.Size = New-Object System.Drawing.Size(300,200) 
$form.StartPosition = "CenterScreen"

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

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20) 
$label.Size = New-Object System.Drawing.Size(280,20) 
$label.Text = "Enter search term"
$form.Controls.Add($label) 

$textBox = New-Object System.Windows.Forms.TextBox 
$textBox.Location = New-Object System.Drawing.Point(10,40) 
$textBox.Size = New-Object System.Drawing.Size(260,20) 
$form.Controls.Add($textBox) 

$form.Topmost = $True

$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $Search = $textBox.Text
    $Search
}
$UserPath = "C:\GetFiles\getfiles"
& cmd /c $UserPath" "$Search

这是您可以处理的一种方法。

$okbutton.enabled = $false # make this a default

if(![string]::IsNullOrEmpty($textbox.text)) #only enable when you have text in the text box
    {
    $okbutton.enabled = $true
    }

输入文本后,您必须使用文本框中的事件将 $okbutton 的值更改为启用。我相信这是您需要的活动: https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.control.textchanged?view=netframework-4.7.2

根据您的工作,您可能还需要其他一些活动。

我明白了。感谢 Thom Schumacher 帮助我完成此操作所需的语法:

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$form = New-Object System.Windows.Forms.Form 
$form.Text = "Enter search criteria"
$form.Size = New-Object System.Drawing.Size(300,200) 
$form.StartPosition = "CenterScreen"

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

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20) 
$label.Size = New-Object System.Drawing.Size(280,20) 
$label.Text = "Enter loan number or date to search"
$form.Controls.Add($label) 

$textBox = New-Object System.Windows.Forms.TextBox 
$textBox.Location = New-Object System.Drawing.Point(10,40) 
$textBox.Size = New-Object System.Drawing.Size(260,20) 
$form.Controls.Add($textBox) 

$form.Topmost = $True

$form.Add_Shown({$textBox.Select()})
do
{
    $result = $form.ShowDialog()
    if  ([string]::IsNullOrEmpty($textbox.text))
        {   
            Write-Warning 'Please enter search term.'
        }
}
until(![string]::IsNullOrEmpty($textbox.text))
    {
    }

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $Search = $textBox.Text
    $Search
}