We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

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

Medium Priority
1,450 Views
Last Modified: 2012-08-14
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?
Comment
Watch Question

Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
>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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Hi

I'm not sure you've got the syntax of that right. frm.visible is always equal to TRUE.
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
if all loaded forms are visible, this is normal (expected) behaviour.

Author

Commented:
Something strange seems to be happening because there is a form that isn't loaded but it is appearing in the Forms collection.
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
>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

Commented:
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
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
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...

Author

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.

Commented:
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 :-)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.