Solved

VB.net Find out if Hidden Form is Open

Posted on 2014-02-21
6
2,337 Views
Last Modified: 2014-02-21
Hi,

How do I find out if a form that is set to "visible = false" is open?

I have tried the following code but it does not work.

Thanks, Greg

Public Function IsFormOpen(ByVal frm As Form) As Boolean
        If Application.OpenForms.OfType(Of Form).Contains(frm) Then
            Return True
        Else
            Return False
        End If
    End Function


Private Sub btnFind_Click(sender As System.Object, e As System.EventArgs) Handles btnFind.Click
        If IsFormOpen(frm_Printers_SearchResult) Then
            MsgBox("open")
        End If
    End Sub

Open in new window

0
Comment
Question by:spen_lang
6 Comments
 
LVL 7

Expert Comment

by:Kishan Zunjare
ID: 39876397
Try with bellow function code;

Public Shared Function isFormOpen(ByVal formName As String) As Boolean
    Dim frm As Form
    Dim i As Integer = 0
    Dim found As Boolean = False
 
    While i < Application.OpenForms.Count
        frm = Application.OpenForms.Item(i)
 
        If frm.Name.ToLower = formName.ToLower Then
            found = True
            Exit While
        End If
 
        i += 1
    End While
 
    Return found
End Function

Open in new window

0
 

Author Comment

by:spen_lang
ID: 39876550
The function returns TRUE if the form is visible but returns FALSE if the forms ".visible = false"
0
 
LVL 12

Accepted Solution

by:
ktaczala earned 500 total points
ID: 39876847
Just set a public variable and check for true or false

For example in form1:

dim frm2 as form2
form2visible = true
frm2.open()

and in frm2 close event
form1.form2visible = false
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 39876905
To what end?...possibly so you don't create another instance when one is already open?

If yes, then don't use the "new" keyword.  Instead, only access the "default instance" of the form, using only the name of it like this:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        frm_Printers_SearchResult.Show()
    End Sub

Open in new window


If you don't like default instances, then follow the singleton pattern.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39876911
Change this

Public Function IsFormOpen(ByVal frm As Form) As Boolean
        If Application.OpenForms.OfType(Of Form).Contains(frm) Then
            Return True
        Else
            Return False
        End If
    End Function

Open in new window



to following

Public Function IsFormOpen(ByVal frm As Type) As Boolean
     For Each openfrm As Form In My.Application.OpenForms
           If TypeOf(openfrm) Is frm Then
               Return True
               Exit For
               Exit Function
     Next
     Return False
End Function

Open in new window



and then call it like

IsFormOpen(GetType(Form1))
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39876913
Ah too late.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 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

15 Experts available now in Live!

Get 1:1 Help Now