Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return the current (active) form

Status
Not open for further replies.

VFP2013

Programmer
Oct 19, 2013
20
US
Hi, while writing my application I wanted to put in a function that when you press escape <ESC> the current form closes. I know there was a way to return the current form number. If you know of a way to do this, please let me know.

 
The current form handles the key press events, no need to find out the current form.

Set any forms KeyPreview = .T., then the form.keypress will get all key events before the current control gets them.

ESC will then be nKeycode = 27 and nShiftAltCtr = 0

Or you do ON ESCAPE Keyboard '{CTRL+F4}' because CTRL+F4 is the shortcut for closing forms, also shown in the forms title bar menu. Then every ESC would cause CTRL+F4 and thus close the active form.

Bye, Olaf.
 
Besides there is _screen.ActiveForm, if that isn't NULL it's referencing the active form,

Bye, Olaf.
 
Yes, but I didn't want to have to code that line for each of my 47 forms.
 
You do ON ESCAPE Keyboard '{CTRL+F4} and it works for any form, it's not bound to a form or datasession or anything.

And in regard to KeyPress event, you implement that in your base form class. And if you don't have created a base form class you're in the boat of many developers rejecting OOP.

Well, this time there is at least two solutions with _SCREEN.ActiveForm and ON ESCAPE, but in a case there is no such solution you really go into each form or other object you didn't inherit from a base class. That's the simple fact.

Bye, Olaf.
 
An easy way of doing this is to add a button to the form, and set its Default property to .T.

When the user presses ESC, the effect is the same as if you clicked the button. So if you write code in the button's Click event which closes the form (such as THISFORM.Release), the form will close when you press ESC.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Well the problem is that I have timers and when someone pushes escape it breaks and interrupts them and displays an error. I just need to surpass this.
 
i highly recommend sub classing your forms before project is too big

Ez Logic
Michigan
 
try this...
Local frm
frm=.null.
DECLARE INTEGER GetFocus IN user32
For each frm in _screen.forms
if frm.hwnd=GetFocus()
exit
endif
Endfor
 
> when someone pushes escape it breaks and interrupts them and displays an error

Actually to break code and display a Program error windows with ****INTERRUPTED is not really an error message, it's just stating what you did: you interrupted your code with ESC. That is the main function of the ESC key in the IDE, to be able tu suspend and debug code.

If your users see this they have one button less in the error messagebox, the SUSPEND button. Because they don't have the debugger.

You SET ESCAPE OFF, as Dave said, to turn off that functionality. But also if you SET ESCAPE ON and ON ESCAPE Keyboard 'CTRL+F4' you turn off the interruption of code and instead let the esc key trigger CTRL+F4.

Bye, Olaf.
 
By the way

>add a button to the form, and set its Default property to .T.
I'd say this button would be the "OK" button and be triggered by ENTER or RETURN key, not ESC.

The ESC key triggers the button click of a button with it's Cancel property set to .T.

Bye, Olaf.
 
*--- Close Active Window
#Define SC_CLOSE 0xF060
#Define WM_SYSCOMMAND 0x112

Declare SHORT PostMessage In user32;
INTEGER HWnd,;
Integer Msg,;
Long wParam,;
Long Lparam
Declare Integer GetFocus In user32

PostMessage(GetFocus(), WM_SYSCOMMAND, SC_CLOSE, 0)
 
Olaf said:
I'd say this button would be the "OK" button and be triggered by ENTER or RETURN key, not ESC.

Your're right, of course, Olaf. I meant that you should set the Cancel property to .T., not the Default property.

In any case, that doesn't answer the point about how to return the "current form number" (whatever that means).

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Yes, but I didn't want to have to code that line for each of my 47 forms.

I'll go out on a limb here and guess that all your forms are based on the native form base class rather than on classes of your own making. While that's not the best way to approach application design, it puts you in a pretty good place to fix your current problem.

Create your customized form base class now, making the keypreview and keypress changes suggested above (and any other behaviors you want to have globally). Then you can use the Class Browser to redefine your forms' parent class, pointing it to your new base class. All of the forms will now inherit behaviors included in their new parent class.

As a bonus, the class browser is scriptable so you could do all 47 forms in one swell foop.

If you'd like to go down this route, we'd all be more than happy to coach you through it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top