I created a ‘log in’ dialog box to log in.
My goal is to do password validation at the moment the user clicks the OK button.
I started using wx.CreateButtonSize, but could not catch the events; so I reverted to making my own OK and Cancel button.
When I click on the OK button, and the password validation fails, I want to prevent the dialog box to close.
I attempted to create a self.Bind(EVT_CLOSE,validate) on the dialog box itself, hoping the event would get called when the OK (or CANCEL button) are clicked, but it is not even fired.
Then I tried to do the validation when the SET_FOCUS is called on the OK button, then use the event.Veto() . But the FocusEvent does not support the Veto().
Then I tried to use the event.Skip(). It blocks the first onfocus event, but when I click the OK button again without having left it, the onfocus event is not fired and I cannot prevent the dialog box from closing.
Can anyone give me some support (preferably with a small example)
Many thanks.
···
Blue Flash wrote:
I attempted to create a self.Bind(EVT_CLOSE,validate) on the dialog box
itself, hoping the event would get called when the OK (or CANCEL button)
are clicked, but it is not even fired.
Why are you binding an event handler to wx.EVT_CLOSE if you want to intercept button clicks? Rather than trying to prevent the dialog from closing when it's already decided it's going to, you need to prevent it from getting to that point in the first place.
Use wx.EVT_BUTTON and perform your validation in your handler. If it succeeds, close the dialog. If not, do nothing, show an error, set focus to the field which has invalid input, etc. And if the user does click Cancel, or decides to close the dialog through some other mechanism, respect their wishes as they obviously don't want to log in right now.
···
--
James Scholes
http://twitter.com/JamesScholes
Hello James,
Thank you so much for bringing me on the right track with your short, to the point feedback.
I was obviously too focused on a method to prevent the dialog box to close, that I overlooked what was just in front of me.
Blue Flash
···
On Sunday, April 9, 2017 at 12:46:30 PM UTC+2, James Scholes wrote:
Blue Flash wrote:
I attempted to create a self.Bind(EVT_CLOSE,validate) on the dialog box
itself, hoping the event would get called when the OK (or CANCEL button)
are clicked, but it is not even fired.
Why are you binding an event handler to wx.EVT_CLOSE if you want to
intercept button clicks? Rather than trying to prevent the dialog from
closing when it’s already decided it’s going to, you need to prevent it
from getting to that point in the first place.
Use wx.EVT_BUTTON and perform your validation in your handler. If it
succeeds, close the dialog. If not, do nothing, show an error, set
focus to the field which has invalid input, etc. And if the user does
click Cancel, or decides to close the dialog through some other
mechanism, respect their wishes as they obviously don’t want to log in
right now.
–
James Scholes
http://twitter.com/JamesScholes