yronium Level: Moderator

 Registered: 14-04-2002 Posts: 907
|
Re: How can I find out if a form is shown or hidden ?
Well, you got to:
- check if the form is loaded or not. I got two small funx to do it, the first returns the forms count, the second returns just whether a form is loaded or not. I usually use the second as "If IsLoaded("frmDetails") Then...". I attached them below.
- if the form is loaded, check if its Visible property is True (you can't bring an hidden form to front)
- if the form is visible, check its WindowsState property is not set on vbMinimize (this check is not required, but I only reported it to complete the speech)
- once you're sure you got your form loaded and visible, and probably not minimized, you can activate it by moving the focus onto it.
Here is a sample, to see it in action, create a project with two forms, put a command button onto the first one and paste the whole code below into the first form module. ' return the count of the loaded instances of a certain form
' if FormCount returns a number grater than zero, the form is loaded
' Usage sample:
' MsgBox "Actually open Customers forms: " & Funx.FormCount("frmCustomers") & " instances"
Public Function FormCount(ByVal frmname As String) As Long
Dim frm As Form
For Each frm In Forms
If StrComp(frm.Name, frmname, vbTextCompare) = 0 Then
FormCount = FormCount + 1
End If
Next frm
End Function
' return True if a form is loaded
Public Function IsLoaded(ByVal frmname As String) As Boolean
IsLoaded = (FormCount(frmname) > 0)
End Function
Private Sub Command1_Click()
' check if the form is already loaded
If IsLoaded("Form2") Then
' the form is already loaded, so there is no need
' of loading it again: we only need to activate it
If Not Form2.Visible = True Then
' if it's loaded but hidden, shows it first (otherwise,
' the next SetFocus method will raise an error)
Form2.Show
' (Note: this instruction is equivalent of settting
' form's Visible property on True)
End If
' moves the focus on the second form
Form2.SetFocus
Else
' if the form is not already loaded, loads it
Load Form2
' (Note: this instruction only loads the form in memory,
' but it still leaves it hidden.
' Of course, I could also show the form in this point,
' instead than just loading it, but not always a loaded form
' is visible, so I have to check the Visible property anyway.
' I didn't load it now in order to show how the above check
' is working)
End If
End Sub | Remember that this behaviour could be affected by other popup forms, as this method doesn't really bring a form in front but moves only the focus onto it, and you know sometimes there are some popup forms which remain in front even without focus. You can't bring your form in front of them anyway. And also, when you have a modal form open you can't move the focus to another one: the bare attempt raises an error.
One final note: when working with MDI forms I always implement a Window menu, with its WindowList property checked. It works like Word's and Excel's Windows menu, taking a constantly updated list of all its open children windows at its bottom, and allowing to switch from one to another.
Hope it helps.
____________________________
Real Programmer can count up to 1024 on his fingers
|