winformspowershellerrorprovider

Powershell use ErrorProvider value for final form validation


With Powershell I have a Windows form, dynamically generating several text boxes. I use a validation on each textbox, then use an ErrorProvider to alert if the validation fails.

This is working fine for displaying the error '!' notification. Is there a way to check how many errors are left?

My pseudo-code would say:

  1. On 'OK' click
  2. Loop though each TextBox
  3. Validate each TextBox
  4. Error if fail / Clear error if pass
  5. Return
  6. If there are no more errors, close the form

Or do I need to maintain a separate logic to see when the errors have been rectified? (The ErrorProvider check below is just a placeholder, I have no idea what to put there!)

$ButtonOK.Add_Click({

    $objectList | where {$_ -is [System.Windows.Forms.TextBox] } | foreach-object {

    Validate-Input $_

    }

    if ($ErrorProvider -eq $null) { #This is where I'm stuck
        $Form.Close()
    }

})

Solution

  • So judging from the response here there is no built in way to reference the amount of errors remaining. Taking the advice of that thread and porting it to Powershell I implemented the following:

    1. Define a hash table to store the error state for each object
    2. When setting or clearing an error use the following:

      Set an error: $errTable.set_item("$($curTB.name)","1")

      Clearing an error: $errTable.remove("$($curTB.name)")

    3. Use the following code in the 'add_click' section:

      If ($errTable.count -le "0") { $form.close() }

    Seems to work well and be pretty shorthand.