Powershell错误验证

时间:2015-05-26 13:54:39

标签: powershell error-handling

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
            }
        }

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

在进行错误验证时,我是否在正确的轨道上?我可以更好地处理它吗?

1 个答案:

答案 0 :(得分:1)

如果要对验证进行完全编程控制,或者需要执行复杂的验证检查,则应使用大多数Windows窗体控件中内置的验证事件。接受自由格式用户输入的每个控件都有一个Validating事件,只要控件需要数据验证就会发生。在Validating事件处理方法中,您可以通过多种方式验证用户输入。看看Event-Driven Validation。这里有更多解释:Extending Windows Forms with a Custom Validation