Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Syntax, Scope or Stupidity?
Message
 
 
To
28/12/2010 12:46:44
General information
Forum:
Visual FoxPro
Category:
Forms & Form designer
Environment versions
Visual FoxPro:
VFP 9 SP1
OS:
Windows XP SP2
Miscellaneous
Thread ID:
01494008
Message ID:
01494026
Views:
59
>This works:
>
>
>FOR EACH loForm In _Screen.Forms
>	 IF loForm.Name = 'OrderEntry_Scx'
>			 	loForm.Release()
>	 ENDIF
>NEXT 
>
>
>But the form to be closed is the second (last) of the forms, it closes the loop. Would this be safe enough? Or should I modify:
>
>*!*	local lnForms, llDone
>*!*	lnForms = _SCREEN.FormCount
>*!*	llDone = .T.
>*!*	DO WHILE lnForms > 0 AND llDone
>*!*	  if pemstatus(_SCREEN.Forms(lnForms), 'QueryUnload', 5) && Toolbars don't have QueryUnload method
>*!*	      llDone = _SCREEN.Forms(lnForms).QueryUnload() 
>*!*	  endif
>*!*	  IF llDone
>*!*	    _SCREEN.Forms(lnForms).Release
>*!*	    lnForms = _SCREEN.FormCount
>*!*	  ENDIF
>*!*	ENDDO
>
>
>
>>>>The reason for this problem as you can not remove an object from collection using FOR EACH loop. This problem was discussed here before and I admit I made the same mistake myself (and may be twice even). You need to loop using counter and from the last record to the first.
>>>>
>>>>Check How can I release all open forms FAQ #7975 It shows the main principle, you need to adapt that code for your case.
>>>
>>>Not sure how the above would be a problem here. Since the intention is only to remove one form then screwing up the further 'FOR EACH' processing shouldn't matter - especially if the FOR EACH loop was exited from within the IF..ENDIF ?
>>>
>>>>And also, as Boris said, you probably need to check name of the form, not caption.
>>
>>Well, even if this may not apply for this particular case (if you exit the loop immediatelly after release()) it is still good practice to avoid the mistake. Since you can not remove members from collection while looping through collection using FOR EACH loop, it's better to learn this once and don't use potentially unsafe code.

If you only need to close one form satisfying the condition, then add exit right after Release() and it will work.

Otherwise, I'd try
for i= screen.Forms.Count to 1 step - 1
    loForm = _screen.Forms(i)
    if loForm.Name = 'Name to search'
       loForm.Release && You may also add code with QueryUnload here to be safe
   endif
next
If it's not broken, fix it until it is.


My Blog
Previous
Reply
Map
View

Click here to load this message in the networking platform