Solved

VB.net Find out if Hidden Form is Open

Posted on 2014-02-21
6
2,311 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
Comment Utility
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
Comment Utility
Ah too late.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

772 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

9 Experts available now in Live!

Get 1:1 Help Now