Powershell错误验证

Powershell错误验证,powershell,error-handling,Powershell,Error Handling,我有一个表单,有6个单选按钮、2个文本框和一个组合框。现在,在错误验证方面,我决定使用一个布尔值并检查文本框是否正确填充,以及是否选择了收件人。填写完所有内容后,将创建一个对象 当涉及到错误验证时,我是否在正确的轨道上?我能处理得更好吗 如果希望对验证进行完全编程控制,或需要执行复杂的验证检查,则应使用大多数Windows窗体控件中内置的验证事件。每个接受自由格式用户输入的控件都有一个验证事件,该事件将在控件需要数据验证时发生。在验证事件处理方法中,可以通过多种方式验证用户输入。看一看。这里还有

我有一个表单,有6个单选按钮、2个文本框和一个组合框。现在,在错误验证方面,我决定使用一个布尔值并检查文本框是否正确填充,以及是否选择了收件人。填写完所有内容后,将创建一个对象


当涉及到错误验证时,我是否在正确的轨道上?我能处理得更好吗

如果希望对验证进行完全编程控制,或需要执行复杂的验证检查,则应使用大多数Windows窗体控件中内置的验证事件。每个接受自由格式用户输入的控件都有一个验证事件,该事件将在控件需要数据验证时发生。在验证事件处理方法中,可以通过多种方式验证用户输入。看一看。这里还有更多的解释:。

听起来好像您正在尝试进行数据验证。此外,如果您的代码确实有效,那么代码复查可能更适合这种情况。@Matt-谢谢。在你看来,我走对了吗?我能更好地处理这个问题吗?你应该在验证事件时添加ScriptBlock。我发布的代码,你看到任何逻辑错误吗?就验证而言,我的思路是正确的?对我来说,验证代码不应该出现在OnClick事件中,但在OnValidating等专用事件中,请查看我答案中的文章。@JPBlanc-你的意思是,每个控件都有自己的偶数处理程序来处理验证?这就是那些文章所建议/演示的,这就是重点。你甚至可以显示一个红色的“!”与相关的消息。感谢链接和帮助。非常感谢。
Function Button_Click()
{
       Param([Parameter(Mandatory=$True)]

            $telephone,
            $calledtoSee,
            $wantstoSeeyou,
            $pleaseCall,
            $willcallAgain,
            $returningYourCall,
            $ToSelection,
            $from,
            $telephoneNumber,
            $message) 


    [boolean]$okayContinue=$true
    [string]$radioSelectedText
    [string]$comboBoxSectionText
    [string]$persontoEmail
    $callType
    $messageCount

     $comboBoxSectionText = $ToSelection.GetItemText($ToSelection.SelectedItem)


     if($okayContinue){

                    if($comboBoxSectionText -eq "Please Select Contact"){
                        [System.Windows.Forms.MessageBox]::Show("Please Select Recipient")
                        $okayContinue=$false

                    }

     }

     if($okayContinue){

                    if([string]::IsNullOrWhiteSpace($from.Text)){
                        [System.Windows.Forms.MessageBox]::Show("Please Enter Who The Message is From")
                        $from.focus()
                        $okayContinue=$false
                    }
    }

    if($okayContinue){

                    if([string]::IsNullOrWhiteSpace($telephoneNumber.Text)){
                        [System.Windows.Forms.MessageBox]::Show("Please Enter Telephone Number")
                        $telephoneNumber.focus()
                        $okayContinue=$false
                   }
     }
     #######################################################################################################################################################

     if($okayContinue){

            if($telephone.Checked){
                    $callType = $telephone.Text
            }
            elseif($calledtoSee.Checked){
                    $callType =  $calledtoSee.Text
            }
            elseif($wantstoSeeyou.Checked){
                    $callType =   $wantstoSeeyou.Text
            }
            elseif($pleaseCall.Checked){
                    $callType= $pleaseCall.Text
            }
            elseif($willcallAgain.Checked){
                    $callType = $willcallAgain.Text
            }
            elseif($returningYourCall.Checked){
                    $callType = $returningYourCall.Text
            }
            else{
                 [System.Windows.Forms.MessageBox]::Show("Please Select Call Type")
                 $okayContinue=$false
            }

    }

    if($okayContinue){

        if([string]::IsNullOrWhiteSpace($message.Text)){

            [System.Windows.Forms.MessageBox]::Show("Please Enter Message")
            $okayContinue=$false
        } 


    }





    if($okayContinue){

            $buildPerson=$comboBoxSectionText.Split(',') 

            $personObject = [pscustomobject]@{

                    FirstName = $buildPerson[0]
                    LastName = $buildPerson[1]
                    Email = $buildPerson[2]
            }

            $messageObject = [pscustomobject]@{

                   Message = $message.Text
                   MessageFor = $personObject
                   From = $from.Text
                   CallType = $callType
                   Telephone = $telephoneNumber.Text
            }
        }