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.
This topic is 5 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
laned101
Posts: 30
Last visit: Thu Sep 23, 2021 1:12 am
Answers: 1

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

Post by laned101 »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

Set "$_.Cancel = $true" In the "Closing" event. The validations should be in the "Closing" or in the validation events.
laned101
Posts: 30
Last visit: Thu Sep 23, 2021 1:12 am
Answers: 1

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

Post by laned101 »

Thanks for the prompt response however, I dont see a closing event function etc?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

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: 30
Last visit: Thu Sep 23, 2021 1:12 am
Answers: 1

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

Post by laned101 »

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

https://ibb.co/gPifue
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

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: 30
Last visit: Thu Sep 23, 2021 1:12 am
Answers: 1

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

Post by laned101 »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

The closing event will fire.
If you don't want to deal with this remove the "X" from the form.
laned101
Posts: 30
Last visit: Thu Sep 23, 2021 1:12 am
Answers: 1

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

Post by laned101 »

Do you have a recommended way to handle the validation with regards to the location it runs? On OKclick event etc?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

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.
This topic is 5 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked