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

Count Child forms on MDI form 2

Status
Not open for further replies.

mejiaks

IS-IT--Management
Jun 9, 2003
131
HN


Hi guys

i am trying to avoid a Child form open twice in my MDI form

how can i find out if an instance of the child form is already open?
i know how to do this when using _screen as MDI, but now i have an own MDI class I want to use

Karben Selim Mejia


LOCAL lnInstances
lnInstances = 0
*:Verificando si ya existe una instancia de la forma a correr. Solo se permite una instancia
FOR EACH loForm IN _screen.Forms
*:Si hy una forma con el mismo nombre
IF This.Name = loForm.Name THEN
lnInstances = lnInstances + 1
IF lnInstances > 1 THEN
MESSAGEBOX(lsTranslate("An instance of this form already open"),16,lsTitulo)
loForm.Show()
RETURN .F.
ENDIF
ENDIF
ENDFOR



Honduras, Central America
The very centre of the world
 
_screen.Forms collection always is filled, even, if you do your own MDI form. _screen.forms() has nothing to do with the forms being children of _screen, every form is maintined in _screen, and even with SCREEN=OFF you have the _screen variable.

You should nevertheless instead use a form handler, that concept is best for maintaining an own collection of form references. The collection class is ideal for this, in VFP8+

Bye, Olaf.
 
Tnx Olaf

My code then should work. for some reason i thought i wouldn't
 
That's true. And maybe there is no reason to change a running system.

With a collection you could simply add forms with their name as key and then loForm = Collection.Item("formname") would either return that form, and you could show it with loForm.Show() or it would cause error 2061, which you can catch with TRY..CATCH or ON ERROR. Also the big plus of the collection is, when adding a form as items, it's automatically remove, when the form closes, so you have something very similar to _screen.forms with an easier way to get a certain form item.

Bye, Olaf.
 
In most of my application, I use a custom Form Manager class. It's essentially a collection of all the open forms. Each time a form opens, it registers itself with the class (via code in the base form class), and it de-registers itself when it closes. The Form Manager has methods for detecting if a given form is open, bringing a specific form to the top, counting the number of open forms, and - most useful of all - broadcasting messages between forms. And quite a lot more.

You might not consider it worth the effort of creating such a class yourself, especially if you don't need all the features. I developed mine several years ago, and I've found it very useful, but using the built-in collection in _SCREEN is also a good approach.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
>using the built-in collection in _SCREEN is also a good approach

Well, let's agree it's not bad. But it really doesn't offer much, only a collection of all forms. The only plus is, it's very reliable in reflecting the current situation, as it's handles by the VFP runtime.

Bye. Olaf.
 
Well, I suppose the advantage of _SCREEN is that the forms don't need to explicitly register or de-register themselves, as they do with my Form Manager. But that's not really a big deal.

By the way, Mejiaks, I assume you don't really display a message box to tell the user that the form is already open. It seems a bit heavy-handed. It's enough simply to bring the form to the front.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Good point Mike, i didn't think that message through

i will remove it

regarding the Form Collection Class. I think it could be helpful when you need a lot of things. but when the only thing you need is one small thing, _screen.forms is quite enough
 
Hi Mejiaks

Keep it simple

*:Verificando si ya existe una instancia de la forma a correr. Solo se permite una instancia
FOR EACH loForm IN _screen.Forms
*:Si hy una forma con el mismo nombre
IF This.Name = loForm.Name THEN
loForm.Show()
ELSE
your code
ENDIF
ENDFOR

MK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top