borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (How do I know if a text file is open and being written on?)Next Topic (Save and Load Database) New Topic New Poll Post Reply
AndreaVB Forum : VB General : How can I find out if a form is shown or hidden ?
Poster Message
iliekater
Level: Master

Registered: 04-02-2005
Posts: 122

icon How can I find out if a form is shown or hidden ?

Is there a way to find out if a form is hidden or not ? I want to do that in order if some of my smaller forms are shown , so that I will bring them on front of the bigger ones so that they will be vissible to the user too . By the way , with what property I am going to move a form in front of the others ?

17-05-2007 at 02:14 PM
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 907
icon 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

17-05-2007 at 09:58 PM
View Profile Send Email to User Show All Posts | Quote Reply
iliekater
Level: Master

Registered: 04-02-2005
Posts: 122
icon Re: How can I find out if a form is shown or hidden ?

Thanks , Yronium . This is helpful .

18-05-2007 at 11:43 AM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB General : How can I find out if a form is shown or hidden ?
Previous Topic (How do I know if a text file is open and being written on?)Next Topic (Save and Load Database) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder