Form question

Posted on 2006-05-24
Last Modified: 2010-04-23
I need to check from Form1 of my application whether Form2 is open.

I tried

Dim f As Form2
 If f Is Nothing Then
end if

but it didn't work
Question by:YZlat
    LVL 5

    Expert Comment

    LVL 35

    Author Comment

    I don't know that. It could be visible or not, open or closed
    LVL 52

    Expert Comment

    by:Carl Tawn
    You are basically asking to check if any instances of Form2 exist. One way to do it would be to add a Shared counter to Form2 and increment/decrement it everytime a reference is created/destroyed.

        Public Class Form2 : Form

            Public Shared Counter As Integer

            Private Sub Form_Load( ... )          '// This might not be the right prototype, couldn't remember it off the top of my head, but hopefully you get the idea
                Form2.Counter += 1
            End Sub

        End Class

    Then from Form1:

        If Form2.Counter > 0 Then    '// We have instances

    I'm sure someone will come along with a better solution, but its an idea at least :)
    LVL 5

    Expert Comment

    does form1 open form 2?
    LVL 69

    Expert Comment

    by:Éric Moreau
    Is your applicatiion a MDI? In this case you can loop through the MDIChildren collection:

        Public Overloads Shared Function IsChildInMemory(ByVal pMainForm As System.Windows.Forms.Form, ByVal pChildName As String, ByRef pReference As Form) As Boolean
            'A small method that checks to see if a particular form is currently displayed as a child form
            Dim frmChild As Form
            For Each frmChild In pMainForm.MdiChildren
                If String.Compare(frmChild.GetType.Name, pChildName, True) = 0 Then
                    pReference = frmChild
                    Return True
                End If

            'On ne l'a pas trouvé
            Return False
        End Function
    LVL 12

    Expert Comment

    If Not IsNothing(f) Then

          'If the Instance still exists.... (i.e. It's Not Nothing)          

          If Not f.IsDesposed Then  
             ' and If it hasn't been disposed yet
             'else it has already been disposed, so you can instanciate a new form
          End If

         'else the form is nothing, so you can safely instanciate a new from
    End If
    LVL 35

    Author Comment

    It worked whne I declared Form2 as Public

    Accepted Solution

    Closed, 50 points refunded.
    Community Support Moderator

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    733 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now