Handling PushButton, TextField and SetFocus on OS X

Hi,

To simplify, I have a sheet window with a TextField and a Save button. If the user edits text in the TextField, keeps the cursor in the TextField and then clicks on the Save button (or hits Return), first the Action event of the Save button executes and then the TextField’s LostFocus event.

I need the LostFocus event of the TextField to fire first, so I can set things before saving.

So I subclass PushButton and put this code in the Save button’s MouseDown event:

me.TrueWindow.SetFocus RaiseEvent MouseDown

And this in the Save button’s Action event:

me.TrueWindow.SetFocus RaiseEvent Action

Is this the proper way to handle this, or do I have a poor design?

Thank you.

Instead of raising the lostfocus event, put your code into a method and call it from the pushbutton’s action event (and possibly from the textfield’s lostfocus event).

Actually, you just gave me another idea.

What if I subclass TextField and add a method to it called, “LostFocus” with the code:

RaiseEvent LostFocus

Then in the window’s Save button, I loop through all the controls of the window and if the control ISA MyTextField, then execute it’s LostFocus method?

However the problem with both of these suggestions is that the LostFocus event of the TextField will always get executed for a second time after the Save button’s Action event finishes.

EDIT: Sorry, you’re solution would not invoke the LostFocus event for a second time. Well, it would still execute after the Save button Action event, but there will be no code in there.

[quote=69778:@Mark S]Actually, you just gave me another idea.

What if I subclass TextField and add a method to it called, “LostFocus” with the code:

RaiseEvent LostFocus

Then in the window’s Save button, I loop through all the controls of the window and if the control ISA MyTextField, then execute it’s LostFocus method?

However the problem with both of these suggestions is that the LostFocus event of the TextField will always get executed for a second time after the Save button’s Action event finishes.

EDIT: Sorry, you’re solution would not invoke the LostFocus event for a second time.[/quote]
Just call the window’s SetFocus method, that should remove focus from the active control, activating it’s LostFocus event.