?
Solved

VB.net Find out if Hidden Form is Open

Posted on 2014-02-21
6
Medium Priority
?
2,485 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 1500 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

770 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