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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

Form question

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
0
YZlat
Asked:
YZlat
1 Solution
 
mydasxCommented:
form2.visible?
0
 
YZlatAuthor Commented:
I don't know that. It could be visible or not, open or closed
0
 
Carl TawnSystems and Integration DeveloperCommented:
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 :)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
mydasxCommented:
does form1 open form 2?
0
 
Éric MoreauSenior .Net ConsultantCommented:
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
                frmChild.Activate()
                pReference = frmChild
                Return True
            End If
        Next

        'On ne l'a pas trouvé
        Return False
    End Function
0
 
Praveen KumarArchitectCommented:
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
         'else it has already been disposed, so you can instanciate a new form
      End If

Else
     'else the form is nothing, so you can safely instanciate a new from
End If
0
 
YZlatAuthor Commented:
It worked whne I declared Form2 as Public
0
 
PAQ_ManCommented:
Closed, 50 points refunded.
PAQ_Man
Community Support Moderator
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now