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

Knowing when a form is visible 2

Status
Not open for further replies.

norason

Programmer
Jan 28, 2009
139
US
Every search I've done regarding how to know when a form is visible says basically the same thing:

If fmMaintenance.Visible = True Then...

but I've tried that and fmMaintenance.Visible is true before it has been called. Also, somehow fmMaintenance is called when I step passed the:

If fmMaintenance.Visible = True Then

line. HUH?

Why is .visible always true and why does it call fmMaintenace?



Public Sub CmdCallFlowChart_Click()
If fmMaintenance.Visible = True Then
Debug.Print "Maintenance is on"
Dim IBeep
For IBeep = 1 To 3 ' Loop 3 times.
Beep ' Sound a tone.
Next
MsgBox "Cannot retrieve charts while Maintenance screen is acive", vbExclamation, "Chart Note"
Exit Sub
End If
Call FmFlowCharts.Show
Exit Sub
End Sub
 
I would iterate through the forms collection, like this:

Code:
    Dim oForm As Form
    
    For Each oForm In Forms
        If oForm.Name = "fmMaintenance" Then
            If oForm.Visible Then
                ' your maintenance form is loaded and visible
            End If
        End If
    Next

This may not be exactly what you are looking for, but it should be a good starting point for you.


-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Checking .Visible will cause the form to load into memory to be able to check the property.

Another way to do it is to use the forms collection and iterate through the loaded forms (without loading them).

Here's a very quick and dirty example to check the state of a seperate form (here called frmVisible):
Code:
Dim blnLoaded As Boolean

'frmVisible.Hide uncomment to load and hide the form

For Each frm In Forms
    If frm.Name = "frmVisible" Then
        blnLoaded = True
        If frm.Visible Then
            Debug.Print "I'm loaded and visible"
        Else
            Debug.Print "I'm loaded and not visible"
        End If
        Exit For
    End If
Next
    
If Not blnLoaded Then Debug.Print "I'm not loaded"
Hope this helps

HarleyQuinn
---------------------------------
You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Beat me again there George [smile], typing the extra branches of the IF statement surely didn't take me the extra 10 minutes... [wink]

HarleyQuinn
---------------------------------
You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
[bigsmile]

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
George and Harley - you guys are incredible! I'll get busy on this code.

Thanks a million!

Gary
 
It worked like a champ!

(I like to use the word champ because we have a 1950 Studebaker Champion convertible that we drive to car shows - sorry for the plug!)


Thanks again!

Gary
 
>Checking .Visible will cause the form to load into memory

Well, only if you use the 'old' way of using forms
 
The 'old' way? Huh?

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Forms in your project are actually GlobalMultiuse classes. That means that you can refer to them by their 'Name' - which is a property - and that causes an instance of the global object to be instantiated (if it doe not alreasdy exist) and accessed. If the global object is already instantiated then you get a reference to that.

So references to Form1 or any of its properties will cause Form1 to appear to reload

In theory the 'new' (well about 10 years old) method of accessing forms is to treat them like a class:

Set MyWorkingForm = New Form1

References to MyWorkingForm or any of its properties will NOT cause Form1 to reload
 
That can't be right.

Module1.bas
Code:
Option Explicit

Private F As Form1

Private Sub Main()
    Set F = New Form1
    MsgBox F.Visible
End Sub
Form1.bas
Code:
Option Explicit

Private Sub Form_Load()
    MsgBox "I was loaded"
End Sub
This results in both MsgBoxes being displayed (first "I was loaded" then "False"), then the process hanging with F loaded but hidden.
 
A stock "Form" from the usual template has the attributes:
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
I think the predeclared ID (object instance) that most people use may be what you're thinking of.
 
There's a quite significantr difference between

Private F As Form1

and

Private F As Form

>That can't be right

Afraid it is though. The only difference is that GlobalMultiUse class gets all it's methods and properties added to the global name space, whilst a form just gets the form added to the global name space.

Apart from that, they both effectively get a hidden declaration like

Class:
Public MyClass As New MyClass

Form:
Public Form1 As New Form1


As for your example, it doesn't illustrate anything unexpected. Here's what I was talking about:
Code:
[blue]Option Explicit

Private F As Form1

Private Sub Main()

On Error GoTo whoops

    ' Old, traditional way that many, many VB programmers learned way back when, and that
    ' most of us continue to use
    MsgBox Form1.Visible
    Unload Form1 ' remove reference from Forms collection
    Set Form1 = Nothing  ' should be last reference, so instance will terminate  ...
    MsgBox Form1.Visible ' Causes reload if form not loaded (and reinit if instance does not currently exist)
                        ' Might be worth pointing out that accessing a form's public variables here would casue a reinit, but not a reload
   
    ' The 'new' way MS wanted us to do it when they introduced VB5
    Set F = New Form1
    MsgBox F.Visible
    Unload F
    Set F = Nothing
    MsgBox F.Visible ' should error, not reload or reinstance
    
  
    'Clean up because we still have references to Form1 in existence, which means the program would
    'keep running ...
    Unload Form1 ' remove reference from Forms collection
    Set Form1 = Nothing  ' should be last reference, so instance will terminate
    Exit Sub
    
whoops:
    If Err = 91 Then
        MsgBox "As expected - no instance exists ..."
    Else
        MsgBox "Ooh - an unexpected error: " & Err.Description
    End If
    Resume Next
    
End Sub[/blue]



 
I can't believe I read through the whole of that code example and didn't see one single reference to a wombat. I am disappointed Mr Strong... [wink]

HarleyQuinn
---------------------------------
You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Traditionally, some of my example code has contained wombats ...

 
Why a wombat, which is a unique Australian animal?(and a rather ugly one too)
 
I can't even begin to remember the origins of its use in my code, I'm afraid. 'tis lost in the mists of time. I started using it somewhere around 1982(ish) ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top