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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form losing Focus and control going back to the previous form:

Status
Not open for further replies.

hunterspot

Programmer
Jun 3, 2009
22
US
Hello Everyone
I have a wierd problem. I am calling a modal form from a modal form. alwaysontop property is False for both forms. When the second form is intiated, it has focus. When I click on a button to activate a few controls (text boxes and checkboxes) the focus is coing back to the first form. Even thoud the second form is visible, it loses focus and not control is clickable. The only way to get out is to click CTRL+ALT+DEL.
When i debugged, I found this to be happening.
The code calling form2 in the form 1 is like this:

code line1.....
Code line2.....
DO Form frmForm2 with param1,parm2,parm3 to lcReturn
Code line4...
Code line 5 ...

When the frmForm2 is visible
_screen.activeform.name is frmForm2
thisform.name is frmForm2

When the 'Modify' button on frmForm2 is clicked and the code is executed

_screen.activeform.name is frmForm2
thisform.name is becoming frmForm1

control is moving to line Code line4 …. in the frmForm1 calling code. It is not stopping at 'Do Form frmForm2...' until the frmForm2 is released.
Can you please help me with this?
Thank you all for your time and help in advance...
 
Why on earth are you nesting modal forms?

By definition, a form should be modal when NOTHING ELSE CAN HAPPEN. The user MUST dispense with the activity of the modal form before anything else can happen. That is the zen of modal forms.

If you have a form where another form should come forward, then the first form should not be modal.

This could have something to do with your problem. Modal has a specific meaning and you're trying to subvert it. There's probably something else going on like a grid grabbing focus or something like that, but it's still not a good idea to nest modal forms.
 
I agree, Dan.

But let's define the zen a little different. A modal form should answer a question, which can have the need to lookup the anwswer in a further dialog (eg you're asked to pick something from a list and you can only decide, if you know a further detail displayed in a sublist).

Technically it should work, even if it isn't the zen of modal form usage.

I tried to reproduce, but thisform.name was not reverting to Form1. First, check what is the name property value of both forms in the form designer. Also the debugger itself may interfere, the debugger is causing lostfocus events, for example. I tried both putting the debugger into the "Foxpro Frame" and the separate "Debug Frame", but that doesn't change anything in my check code. What about ActiveX controls on the forms?

I did just this in a button click of Form2:
lcActiveformName = _Screen.ActiveForm.Name
lcThisformName = Thisform.Name
Set Step On

Inspecting the variable values and hovering the mouse over the right side object properties yields the same "Form2" value in all 4 cases.

Something is interfering, a timer, activex, bindevent, whatever. You have to find out, or rethink the overall design. You can also let a form wait for the result of a nonmodal form without using TO lcReturnvalue. You may pass in THISFORM (frmForm1) to frmForm2 as one additional parameter and a method to callback as second additional paramter, the secondary form then may call that method eg via storing the form parameter into form2.oCaller and store the method to call back as form2.Callback, then in unload or release set lcReturnvalue = "return value" and finally =Evaluate('thisform.oCaller.'+thisform.Callback+'(lcReturnvalue)')

Bye, Olaf.
 
Dan, just to be clear, can you explain what you mean by "nesting" modal forms? Do you mean that one modal form shouldn't call another modal form? I assume not, because that's quite a common thing to do.

As far as the original question is concerned, it looks as if there's something in the "Modify" code that is causing Form2 to relinquish control (to pass control to the line after the DO FORM). But that wouldn't explain why _screen.activeform is still pointing to Form2.

Hunterspot, can you post the code from the "Modify" button - from its Click event, that it - and also any other code that might fire at that point, such as the button's GotFocus or MouseDown?

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Dan
I tried modifying the forms to be Modless but that didn't work

Mike
Here is the code in the 'Modify' button.

***********************************

WITH THISFORM
SET STEP ON
IF NOT .lApplyFinal
thisform.pageframe1.page1.Enabled = .T.
thisform.pageframe1.page2.Enabled = .T.
= thisform.enablepage1()
= thisform.enablepage2()

thisform.enable_find()
ELSE
IF oApplicationObject.GetUserType() = [Underwriter]

thisform.pageframe1.page1.Enabled = .T.
thisform.pageframe1.page2.Enabled = .T.
= thisform.enablepage1()
= thisform.enablepage2()

thisform.enable_find()
ELSE
THISFORM.enable_disable1.DISABLE()
thisform.disbale()
thisform.pageframe1.page1.Enabled = .F.
thisform.pageframe1.page2.Enabled = .F.
THISFORM.pageframe1.page1.lblUWinfo.Enabled=.f.
WAIT WINDOW [Reviewed by Underwriter] NOWAIT
THISFORM.cmdsave1.Enabled = .T.

thisform.pageframe1.page1.txtorigEffDate.Enabled= .F.
ENDIF
ENDIF
IF curboundProposal.newgroup = .F.
thisform.pageframe1.page1.txtorigEffDate.Enabled = .T.
thisform.pageframe1.page1.label5.Enabled = .T.
ELSE
thisform.pageframe1.page1.txtorigEffDate.Enabled = .F.
thisform.pageframe1.page1.label5.Enabled = .F.
ENDIF
thisform.pageframe1.page1.txtPropReqdate.Enabled = .F.
ENDWITH
thisform.pageframe1.page1.lblUWinfo.enabled = .F.
thisform.pageframe1.page1.lblStatus.enabled = .F.


IF oApplicationObject.GetUserType() = [Underwriter]
thisform.pageframe1.page2.txtACTVLOCONT.Enabled = .T.
ELSE
thisform.pageframe1.page2.txtACTVLOCONT.Enabled = .F.
ENDIF
WITH THISFORM
IF INLIST(UPPER(ALLTRIM(curboundproposal.status)),[SOLD],[NOT SOLD],[CANCELLED])
THISFORM.pageframe1.page1.chkrfw.Enabled = .F.
ELSE
THISFORM.pageframe1.page1.chkrfw.Enabled = .T.
ENDIF
ENDWITH

WITH THISFORM
IF oApplicationObject.GetUserType() = [Underwriter]

IF curBoundProposal.newgroup = .T.
IF DATE() - curBoundProposal.prop_reqdt < 91

ELSE

ENDIF
ENDIF

IF curBoundProposal.newgroup = .F.
IF DATE() - curBoundProposal.prop_reqdt < 121
* .cmdgrps.Enabled = .T.
ELSE
* .cmdgrps.Enabled = .F.
ENDIF
ENDIF
ELSE
IF DATE() - curBoundProposal.prop_reqdt < 91
* .cmdgrps.Enabled = .T.
ELSE
* .cmdgrps.Enabled = .F.
ENDIF
ENDIF
ENDWITH
WITH THISFORM
IF UPPER(ALLTRIM(curboundproposal.status)) == [CANCELLED]
THISFORM.pageframe1.page1.lblStatus.Enabled = .T.
ELSE
THISFORM.pageframe1.page1.lblStatus.Enabled = .F.
ENDIF
ENDWITH

THISFORM.cmdgrps.Enabled = .T.
thisform.cmdClasses.Enabled = .T.
thisform.cmdconts.Enabled = .T.
thisFORM.cmdCarr.Enabled = .T.
THIS.Enabled = .F.
THISFORM.pageframe1.page1.chkNewGroup.Enabled = .F.
thisform.llprocessgoingon = .T.

IF curBoundProposal.smallgroup = .T.
thisform.pageframe1.page1.txtrenwdt.Enabled = .F.
ELSE
thisform.pageframe1.page1.txtrenwdt.Enabled = .T.
ENDIF
WITH THISFORM
IF INLIST(UPPER(ALLTRIM(thisform.oCompanyRecord.county)),[BENTON],[CARROLL],[BOONE],[MARION],[BAXTER],[SEARCY],[NEWTON],[WASHINGTON],[MADISON])
IF curBoundProposal.smallgroup = .F.
thisform.pageframe1.page1.chkunbranded.Visible = .T.
thisform.pageframe1.page1.chkunbranded.Enabled = .T.
ENDIF
ELSE
thisform.pageframe1.page1.chkunbranded.Visible = .F.
thisform.pageframe1.page1.chkunbranded.Enabled = .F.
ENDIF
ENDWITH

IF curBoundProposal.effdtereq < {04/01/2010}
IF thisform.UserMode = [ADD]
IF curBoundProposal.unbranded = .T.
replace phcs WITH .T. in curBoundProposal
thisform.pageframe1.page1.checkphcs.Enabled = .F.
ELSE
IF UPPER(ALLTRIM(thisform.ocompanyrecord.st)) <> [TX]
replace phcs WITH .F. in curBoundProposal
thisform.pageframe1.page1.checkphcs.Enabled = .F.
ELSE
thisform.pageframe1.page1.checkphcs.Enabled = .T.
ENDIF
ENDIF
ENDIF
ELSE
replace phcs WITH .F. in curBoundProposal
thisform.pageframe1.page1.checkphcs.Enabled = .F.
thisform.pageframe1.page1.checkphcs.Visible = .F.
ENDIF

IF INLIST(UPPER(SUBSTR(SYS(0), AT("#", SYS(0)) + 2)),[RLCLAYTON],[MAJAMES],[MKDAVULURI])
IF thisform.cmdsave1.Enabled = .T.
thisform.pageframe1.page1.chkovrbenroll.Enabled = .T.
ELSE
thisform.pageframe1.page1.chkovrbenroll.Enabled = .F.
ENDIF
ELSE
thisform.pageframe1.page1.chkovrbenroll.Enabled = .F.
ENDIF
IF curBoundProposal.effdtereq < {10/15/2009}
thisform.pageframe1.page1.chkmHParity.Enabled = .F.
thisform.pageframe1.page1.chkmHParity.value = .F.
replace mhparity WITH .F. in curboundproposal
ENDIF
IF thisform.usermode = [ADD]
IF INLIST(UPPER(ALLTRIM(thisform.oCompanyRecord.county)),[BENTON],[CARROLL],[BOONE],[MARION],[BAXTER],[SEARCY],[NEWTON],[WASHINGTON],[MADISON])
IF curBoundProposal.smallgroup = .F.
thisform.pageframe1.page1.chkunbranded.Visible = .T.
thisform.pageframe1.page1.chkunbranded.Enabled = .T.
ENDIF
ELSE
thisform.pageframe1.page1.chkunbranded.Visible = .F.
thisform.pageframe1.page1.chkunbranded.Enabled = .F.
ENDIF
ENDIF
IF thisform.usermode = [EDIT]
IF INLIST(UPPER(ALLTRIM(thisform.oCompanyRecord.county)),[BENTON],[CARROLL],[BOONE],[MARION],[BAXTER],[SEARCY],[NEWTON],[WASHINGTON],[MADISON])
IF curBoundProposal.smallgroup = .F.
thisform.pageframe1.page1.chkunbranded.Visible = .T.
thisform.pageframe1.page1.chkunbranded.Enabled = .T.
ENDIF
ELSE
thisform.pageframe1.page1.chkunbranded.Visible = .F.
thisform.pageframe1.page1.chkunbranded.Enabled = .F.
ENDIF
ENDIF
IF curboundProposal.directacct = .T.
THISFORM.pageframe1.page1.txtfirst2.Enabled = .F.
THISFORM.pageframe1.page1.txtlast2.Enabled = .F.
THISFORM.pageframe1.page1.txtlicense2.Enabled = .F.
THISFORM.pageframe1.page1.txtfedtaxid2.Enabled = .F.
THISFORM.pageframe1.page1.txtsocialsecurity2.Enabled = .F.
ENDIF
SET STEP ON
thisform.pageframe1.page1.SetFocus()
********************
Thanks
 
First of all how about cleaning up some of that code? Why use THISFORM within a WITH THISFORM...ENDWITH block, for one thing?

At what line of that modify button click code does thisform.name switch? From the beginning? Have you inspected the value of the name property? Is it perhaps "frmForm1" from the beginning, already in the design of that form? The form.name is quite irrelevant, it doesn't tell you, what object you really have at hand. If you never set the name, all your SCX forms will have the name "Form1".

_screen.activeform.name and thisform.name should not differ anyway, but one possibility is, that _screen.activeform is calling some method in another form, then debugging that method makes thisform.name differ from the activeform. thifrom is always from the persepcive of the code location, not from the perspective of the active form. So that might be involved. It can differ.

Bye, Olaf.
 
Dan, just to be clear, can you explain what you mean by "nesting" modal forms? Do you mean that one modal form shouldn't call another modal form? I assume not, because that's quite a common thing to do.

That's exactly what I mean, and being common does not make it right. [wink] We've (all of us) been abusing modal forms for years.

Here's an example. Create a menu in VFP. Open the General options dialog. It is a modal dialog, meaning you can't open anything else. Click the checkboxes (!) for Setup... and Cleanup... and note that they open code editing windows, but you can't get to them until you have dispatched the modal dialog. But make sure you click OK to close the dialog because any other result closes the editing windows you went there to open in the first place. It's a cumbersome enough contrivance that I find it annoying every time I use it.

(I've whined about the mis-use of checkboxes for this since FPD, too. Checkboxes should turn things on or off. They shouldn't launch actions. And if there is already code, THEY SHOULD ALREADY BE CHECKED. This one is a double whammy of ugly U/I design.)

It has been that way for so long you might call it common. I call it WRONG. <g>

Why foist that on your own users?
 
Dan,

I'm a bit puzzled by this.

I do agree with you about checkboxes not launching actions. I've moaned about that myself many times. (Another example is the Attach Icon checkbox in the Project Information dialogue.)

On the wider question of modal forms, the menu designer is a lousy example. It's got so many things wrong with it, UI-wise, that you shouldn't draw any lessons from it.

But to go back to the original question. You said that the fact that Hunterspot has nested modal forms could be the cause of his problem. In other words, the problem might be the result of a modal form calling another form. But you haven't said why.

I agree that, just because it's common, that doesn't make it right. But why is it not right?

Basically, what you are saying is that only modeless forms can launch other forms. I can only say that this is the first time I've come across that assertion - which is why I'd like to understand why you are insisting on it.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Well, Dan, that is so wrong in many ways, but the reason you can't activate the edit windows is, they are not modal. Of course you should never call nonmodal forms from modal forms. Your point in not starting anything else also is taken, nevertheless calling a modal from from a modal form simply allows you to extend the modal dialog with a further modal dialog instead of including it on the first modal form.

hunterspot said:
I tried modifying the forms to be Modless but that didn't work

You can't simply set the form nonmodal and expect the DO FORM ... TO lcReturnValue to work.

I sketched a way to do it, please reread the last paragraph of my answer from before your posting of the Modify button code. It works with all forms nonmodal, which is the ideal way to solve this without using DO FORM TO Variable at all.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top