How to determine if an MDI child is open in VB6?

Hi

I need to be able to determine if an MDI child form is already open.

I have the following function

Public Function IsLoaded(frmName As String) As Boolean
Dim frm As Form
   
        For Each frm In Forms
             If frm.Name = frmName Then
                     IsLoaded = True
                     Exit Function

              End If
   
        Next

        IsLoaded = False
End Function

The problem with this is that it loops through all forms in the project, not just the open forms, thus always returning TRUE from the function.

How do I get the function to only loop through forms that are open?
advantagecomputersAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
>not just the open forms
this loops through all the LOADED forms, even those are not visible.
if you have forms that are not loaded, they will NOT be in the loop.

for example, you might have closed the mdi child form, but still have a reference to it, reloading it but keeping it invisible.


you might try out this:

Public Function IsLoadedAndVisible(frmName As String) As Boolean
Dim frm As Form
   
        For Each frm In Forms
             If frm.Name = frmName Then
                     IsLoadedAndVisible = frm.Visible
                     Exit Function

              End If
   
        Next

        IsLoadedAndVisible= False
End Function
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
advantagecomputersAuthor Commented:
Hi

I'm not sure you've got the syntax of that right. frm.visible is always equal to TRUE.
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
if all loaded forms are visible, this is normal (expected) behaviour.

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

advantagecomputersAuthor Commented:
Something strange seems to be happening because there is a form that isn't loaded but it is appearing in the Forms collection.
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>form that isn't loaded but it is appearing in the Forms collection.
that is not "possible"
as I said, form a form you have 3 states:
* defined   ---> this is only the form's class module definition
* loaded   ---> dim f as YourForm  :  set f = new Yourform  'this defined a form variable and loads it (but does NOT show it)
* visible   --->  dim f as YourForm  :  set f = new Yourform : f.show   'this full loads AND displays the form

only forms that are AT LAST loaded will be in the forms collection, this includes forms that are NOT visible

you can try this out by debugging through this loop:

dim f as form

for each f in Forms
   if not f.visible then
    debug.print f.name
    f.show  <== put a breakpoint on this line, and see what happens if you run this line (F8)
 
   end if

next
0
SethiCommented:
I use the following code in all my MDI application. What I do is that when I can a form, I unload all other forms before displaying the called form:

Public Sub ShowForm(strShowForm As String, Optional Hide As Boolean, Optional strTitle As String)
    On Error GoTo Err
        If g_strActiveForm <> strShowForm Then
            Screen.MousePointer = vbHourglass
            Set objActiveForm = mdiParent.ActiveForm
            If Hide = True Then
                Forms.Add(strShowForm).Show
                objActiveForm.Hide
            Else
                LockWindowUpdate mdiParent.hWnd
                Forms.Add(strShowForm).Show
                Unload objActiveForm
                For intLoop = 0 To 50
                    DoEvents
                Next
                LockWindowUpdate 0
            End If
            Screen.MousePointer = vbDefault
        Else
            If strTitle <> "" Then
                Set objActiveForm = mdiParent.ActiveForm
                Forms.Add(strShowForm).Show
            End If
        End If
    Exit Sub
Err:
    If Err.Number = 365 Then
        Screen.MousePointer = vbDefault
        Resume Next
    End If
End Sub
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
if guess this part is wrong:

        Else
            If strTitle <> "" Then
                Set objActiveForm = mdiParent.ActiveForm
                Forms.Add(strShowForm).Show
            End If
        End If

it should be:

        Else
            If strTitle <> "" Then
                Set objActiveForm = mdiParent.ActiveForm
                Forms(strShowForm).Show
            End If
        End If

as otherwise, you readd the active form again and again...
0
advantagecomputersAuthor Commented:
Hi

I've got it going, here was what the problem was.

When I was calling the function to check if a form was open I was specifyting IsLoaded(frm123.Name) rather than IsLoaded("frm123"). This was actually adding frm123 to the Forms collection, so IsLoaded would always return TRUE.

Thanks for your help.
0
SethiCommented:
angelIII: if guess this part is wrong....

No my friend that part is correct. The form is automatically added into the collection once it opens. This code is being used by me for last 3 years in all my MDI projects, so it is working too :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.