Abort GUI 'OK' click due to validation of form input

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.
laned101
Posts: 16
Joined: Thu Aug 16, 2018 3:36 am

Abort GUI 'OK' click due to validation of form input

Post by laned101 » Fri Aug 31, 2018 3:55 pm

Im carrying out validation in the `$buttonOK_Click` block and if theres an error, it should prompt (this it does). Once clicking 'OK' on the error you should be able to modify input before submitting again.

However Im not entirely sure how to exit the block to stop the form closinging and to stop the main() continueing. Ive tried using a break, returns and even tried the same breakout attempt in the main() form but with no luck.

Any help would be apprecaited.

https://ibb.co/hWRBXz
Image

***Code Block***
https://ibb.co/nQJfQK
Image

User avatar
jvierra
Posts: 13687
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Abort GUI 'OK' click due to validation of form input

Post by jvierra » Fri Aug 31, 2018 4:03 pm

Set "$_.Cancel = $true" In the "Closing" event. The validations should be in the "Closing" or in the validation events.

laned101
Posts: 16
Joined: Thu Aug 16, 2018 3:36 am

Re: Abort GUI 'OK' click due to validation of form input

Post by laned101 » Fri Aug 31, 2018 5:25 pm

Thanks for the prompt response however, I dont see a closing event function etc?

User avatar
jvierra
Posts: 13687
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Abort GUI 'OK' click due to validation of form input

Post by jvierra » Fri Aug 31, 2018 6:37 pm

Just look at the events list under the form. It is called "FormClosing" and is in the "Behavior" group.

See: https://info.sapien.com/index.php/guis/ ... rm-control

laned101
Posts: 16
Joined: Thu Aug 16, 2018 3:36 am

Re: Abort GUI 'OK' click due to validation of form input

Post by laned101 » Sat Sep 01, 2018 10:17 am

I dont see that event on the form event list. Maybe because I dont have a cancel button?

https://ibb.co/gPifue

User avatar
jvierra
Posts: 13687
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Abort GUI 'OK' click due to validation of form input

Post by jvierra » Sat Sep 01, 2018 11:14 am

You won't see it until you add it.
Look up how to add events in the manual. There are a number of ways to do this. THe easiest is to right click on the form and select Add Events".

laned101
Posts: 16
Joined: Thu Aug 16, 2018 3:36 am

Re: Abort GUI 'OK' click due to validation of form input

Post by laned101 » Sat Sep 01, 2018 12:00 pm

Arrr I see! Thank you, out of curiousity is there something to identify if the X to close the window is pressed? As im doing validation checks in the closing event I get validation failures if I close the form on the X too. So the form doenst close on the X press

User avatar
jvierra
Posts: 13687
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Abort GUI 'OK' click due to validation of form input

Post by jvierra » Sat Sep 01, 2018 12:20 pm

The closing event will fire.
If you don't want to deal with this remove the "X" from the form.

laned101
Posts: 16
Joined: Thu Aug 16, 2018 3:36 am

Re: Abort GUI 'OK' click due to validation of form input

Post by laned101 » Sat Sep 01, 2018 2:25 pm

Do you have a recommended way to handle the validation with regards to the location it runs? On OKclick event etc?

User avatar
jvierra
Posts: 13687
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Abort GUI 'OK' click due to validation of form input

Post by jvierra » Sat Sep 01, 2018 2:30 pm

Validation only runs in validation events. You seem to be missing how events work.

There are a great may articles that describe various approaches to validation. We have to design a validation mechanism that applies to the task we are performing and the event structure of forms.

Locked