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

Form ExitButton (X) action

Post by laned101 » Sat Sep 01, 2018 3:51 pm

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: 13881
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Form ExitButton (X) action

Post by jvierra » Sat Sep 01, 2018 3:55 pm

Remove the control box from the form.

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

Re: Form ExitButton (X) action

Post by laned101 » Sat Sep 01, 2018 5:18 pm

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: 13881
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Form ExitButton (X) action

Post by jvierra » Sat Sep 01, 2018 5:22 pm

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

Re: Form ExitButton (X) action

Post by laned101 » Sun Sep 02, 2018 2:15 am

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: 13881
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Form ExitButton (X) action

Post by jvierra » Sun Sep 02, 2018 2:43 am

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

Re: Form ExitButton (X) action

Post by laned101 » Sun Sep 02, 2018 11:42 am

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: 13881
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Form ExitButton (X) action

Post by jvierra » Sun Sep 02, 2018 11:48 am

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?

Locked