Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2006-04-03
9
Medium Priority
?
1,122 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?
0
Comment
Question by:advantagecomputers
  • 4
  • 3
  • 2
9 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 16359039
>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
 

Author Comment

by:advantagecomputers
ID: 16359101
Hi

I'm not sure you've got the syntax of that right. frm.visible is always equal to TRUE.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16359240
if all loaded forms are visible, this is normal (expected) behaviour.

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:advantagecomputers
ID: 16359375
Something strange seems to be happening because there is a form that isn't loaded but it is appearing in the Forms collection.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16359524
>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
 
LVL 18

Expert Comment

by:Sethi
ID: 16360036
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
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16360072
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
 

Author Comment

by:advantagecomputers
ID: 16360091
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
 
LVL 18

Expert Comment

by:Sethi
ID: 16360978
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

575 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question