我无法解决验证事件。我创建了一个非常简单的窗体来演示这个问题。该表单包含两个文本框,我希望在第一个文本框中按Tab键时触发验证事件,但事实并非如此。我正在使用在Windows 7 Professional SP1下运行的Powershell V4.0。代码如下: -
Function ValidateField( [string]$FieldValue )
{
Write-Host "ValidateField: Function Entered"
if ( $FieldValue -eq $Null -Or $FieldValue.Trim().Length -eq 0 ) { Return $True } else { Return $False }
}
Function GenerateForm
{
Write-Host "GenerateForm: Function Entered"
[Reflection.Assembly]::LoadWithPartialName( "System.Windows.Forms" ) | Out-Null
[Reflection.Assembly]::LoadWithPartialName( "System.Drawing" ) | Out-Null
$Form = New-Object System.Windows.Forms.Form ; $IFWS = New-Object System.Windows.Forms.FormWindowState
$Code = New-Object System.Windows.Forms.TextBox ; $Desc = New-Object System.Windows.Forms.TextBox
$Form.ClientSize = New-Object System.Drawing.Size( 400,200 )
$Form.StartPosition = "CenterScreen" ; $Form.add_Load( $FormLoad )
$OnLoadForm_StateCorrection = { $Form.WindowState = $IFWS }
$Code.DataBindings.DefaultDataSourceUpdateMode = 0 ; $Code.Location = New-Object System.Drawing.Size( 10,10 )
$Code.Size = New-Object System.Drawing.Size( 40,20 ) ; $Code.CausesValidation = $True
$Code_Validating = [System.ComponentModel.CancelEventHandler] {
{ $Result = ( ValidateField $Code.Text )
if ( $Result -eq $True ) { $_.Cancel = $True } }
}
$Form.Controls.Add( $Code )
$Desc.DataBindings.DefaultDataSourceUpdateMode = 0 ; $Desc.Location = New-Object System.Drawing.Size( 100,10 )
$Desc.Size = New-Object System.Drawing.Size( 169,20 ) ; $Form.Controls.Add( $Desc )
$IFWS = $Form.WindowState ; $Form.add_Load( $OnLoadForm_StateCorrection ) ; $Form.ShowDialog() | Out-Null
}
GenerateForm
我可能只是错过了一些非常简单的事情 - 但这已经让我疯了3天了,所以任何帮助都会感激不尽。
答案 0 :(得分:0)
我看不到您要连接到Validating
事件的位置。用以下代码替换$ Code_Validating作业:
$Code.add_Validating({
$result = ValidateField $sender.Text
if ($result) {$eventArgs.Cancel = $true}
})