better way to validating a control??

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Post Reply
paddy75
Posts: 18
Joined: Fri Sep 04, 2020 7:59 am

better way to validating a control??

Post by paddy75 »

To help you better we need some information from you.

*** Please fill in the fields below. If you leave fields empty or specify 'latest' rather than the actual version your answer will be delayed as we will be forced to ask you for this information. ***

Product, version and build:PowerShell Studio 2020, Version 5.7.182
Operating system:Win10 x64
PowerShell version(s):5.1

*** Please add details and screenshots as needed below. ***

DO NOT POST LICENSES, KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM




Hello all,

I'm playing a little with the errorprovider and how to validate a textbox.
I have a form with one textbox and one button. When the textbox is a valid email address, the button should be enabled, if not it should be disabled.
The problem what i have is, that the validating event only occours when you leave the textbox. But this is not possible when you have no other controls on it. Here is the example code what i have at the moment. Is there a better/other way to do this?
  1. $textboxEmail_Validating=[System.ComponentModel.CancelEventHandler]{
  2.  
  3. #Event Argument: $_ = [System.ComponentModel.CancelEventArgs]
  4.  
  5.     $result = -not (Test-IsEmail $textboxEmail.Text)
  6.  
  7.    
  8.  
  9.     If ($result -eq $true)
  10.  
  11.     {
  12.  
  13.         $errorprovider1.SetError($this, "Invalid Email address");
  14.  
  15.         $button1.Enabled = $false
  16.  
  17.     }
  18.  
  19.     Else
  20.  
  21.     {
  22.  
  23.         $errorprovider1.SetError($this, "");
  24.  
  25.         $button1.Enabled = $true
  26.  
  27.     }
  28.  
  29. }
  30.  
  31.  
  32.  
  33. Function Test-IsEmail ([string]$Email)
  34.  
  35. {
  36.  
  37.     return $Email -match "^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$"
  38.  
  39. }
  40.  
  41.  

User avatar
brittneyr
Site Admin
Posts: 645
Joined: Thu Jun 01, 2017 7:20 am
Answers: 8
Been upvoted: 6 times

Re: better way to validating a control??

Post by brittneyr »

[Topic moved to PowerShell GUIs forum by moderator]
Brittney Ryn
SAPIEN Technologies, Inc.

jvierra
Posts: 14672
Joined: Tue May 22, 2007 9:57 am
Answers: 6
Has voted: 1 time
Been upvoted: 5 times
Contact:

Re: better way to validating a control??

Post by jvierra »

You will have to do it like the following example attached.
Attachments
Test-TextValidate.psf
(16.97 KiB) Downloaded 158 times

Post Reply