1
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?

3
  • It sounds like you are trying to do Data Validation. Also if your code actually does work Code Review might be a better fit for this. Commented May 26, 2015 at 14:07
  • @Matt - Thanks. In your opinion, am I on the right track? Could I be handling this better? Commented May 26, 2015 at 14:09
  • You should add ScriptBlocks on OnValidating events. Commented May 26, 2015 at 14:15

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

The code that I've posted, do you see any logic errors? I'm I on the right track in terms of validation?
For me the validation code should not be in OnClick event, but in dedicated events like OnValidating, have a look to the articles in my answer.
@JPBlanc- You mean, each control has its own even handler to handle validation? That's what those articles seem to suggest/demonstrate
Yes, that's the point. You can even show a red '!' with an associated message.
Thanks for the links and help. Much appreciated.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.