Solved

VB.net Find out if Hidden Form is Open

Posted on 2014-02-21
6
2,417 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 86

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET Built-In Report Creator / Viewer 5 41
VB.net Is there code behind PowerPivot 2 70
vb.net dbnull syntax 1 56
Shared Service Environment 2 54
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It’s quite interesting for me as I worked with Excel using vb.net 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 …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

734 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