user4350786
user4350786

Reputation:

Powershell Error Validation

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

I've got a form with 6 radio buttons, and 2 text boxes, and a combobox. Now, in terms of error validation, I decided to use a boolean value and check to see that the textboxes are properly filled and that a recipient has been selected. After everything has been filled in, then an object is created.

Am I on the right track when it comes to error validation? Could I be handling it better?

Upvotes: 1

Views: 219

Answers (1)

JPBlanc
JPBlanc

Reputation: 72610

If you want full programmatic control over validation, or need to perform complex validation checks, you should use the validation events built into most Windows Forms controls. Each control that accepts free-form user input has a Validating event that will occur whenever the control requires data validation. In the Validating event-handling method, you can validate user input in several ways. Have a look to Event-Driven Validation. And More explanations here : Extending Windows Forms with a Custom Validation.

Upvotes: 1

Related Questions