Form ExitButton (X) action

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

Form ExitButton (X) action

Post by laned101 »

Im running input validation checks on FormClosing event, this means if I try close the form via the Exit button (top right) I hit validation issues. Is there a way to check if the X button has been clicked to bypass the validation on Exit button click?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Form ExitButton (X) action

Post by jvierra »

Remove the control box from the form.
laned101
Posts: 30
Last visit: Thu Sep 23, 2021 1:12 am
Answers: 1

Re: Form ExitButton (X) action

Post by laned101 »

Then I wont be able to close the form unless I complete all fields. Is there a way to check if any button has been clicked? Surely there must be a bool somewhere?

I tried defining a bool variable in GLobals then setting it to true in th OKButtonclick event but when querying it in the closing event it reads incorrectly.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Form ExitButton (X) action

Post by jvierra »

I posted an example of how to close and cancel a form. You cannot detect if the "X" has been clicked. The code posted shows you how to deal with the closing issue. If you need something else then you will have to learn how forms work and ask a better question.

You can set a variable in any event and that will be visible anywhere in the form and in any event. You have to "scope" the variable when assigning it or the change will be lost when the event exits.
laned101
Posts: 30
Last visit: Thu Sep 23, 2021 1:12 am
Answers: 1

Re: Form ExitButton (X) action

Post by laned101 »

So I'm keeping the exit button, I just defined the scope for the variable in Globals and its working as expected now.

Although your comments are a little dismissing and presumptuous I do appreciate the help. A ref to your "post" would have been useful however, the mention of scope was in fact useful. Thank you
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Form ExitButton (X) action

Post by jvierra »

I attached a full example for everything you asked in the beginning. I couldn't understand why you kept asking the same question after I had answered it three different ways.

Without a good understanding of PowerShell basics using form will be a challenge. Also look at the articles and examples in the info center to get a better idea of how forms work.
laned101
Posts: 30
Last visit: Thu Sep 23, 2021 1:12 am
Answers: 1

Re: Form ExitButton (X) action

Post by laned101 »

Yes I'm not familiar with forms, but I have some experience in Powershell but mostly python, Ive resolved the problem nonetheless. I wanted to keep the control box and as this is the first form Ive ever created I wasn't sure on the best location to carry things like validation but now I know the flow a bit better, and understand the event triggers (im new to GUIs in generally, always been a terminal guy) Ive setup input validation on all fields (multiple child forms), setup local logging and error output, terminal logging (gui). I will setup a SYSLOG function on specific events next. Thanks again
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Form ExitButton (X) action

Post by jvierra »

So then you understand that you cannot detect that the "X" has been clicked. You can only detect a button click and use that to eliminate the issue?
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